forked from google/crubit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpragma_test.cc
More file actions
123 lines (108 loc) · 3.56 KB
/
pragma_test.cc
File metadata and controls
123 lines (108 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Part of the Crubit project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include "nullability/pragma.h"
#include <memory>
#include <optional>
#include "clang/AST/ASTConsumer.h"
#include "clang/Basic/Diagnostic.h"
#include "clang/Basic/SourceLocation.h"
#include "clang/Basic/Specifiers.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/FrontendActions.h"
#include "clang/Testing/TestAST.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Error.h"
#include "external/llvm-project/third-party/unittest/googlemock/include/gmock/gmock.h"
#include "external/llvm-project/third-party/unittest/googletest/include/gtest/gtest.h"
namespace clang::tidy::nullability {
namespace {
using ::testing::ElementsAre;
using ::testing::IsEmpty;
using ::testing::Pair;
using ::testing::UnorderedElementsAre;
class PragmaTest : public ::testing::Test {
protected:
std::optional<TestAST> AST;
TestInputs Inputs;
NullabilityPragmas Pragmas;
PragmaTest() {
Inputs.FileName = "main.cc";
Inputs.MakeAction = [&] {
struct Action : public SyntaxOnlyAction {
NullabilityPragmas &Pragmas;
Action(NullabilityPragmas &Pragmas) : Pragmas(Pragmas) {}
std::unique_ptr<ASTConsumer> CreateASTConsumer(
CompilerInstance &CI, llvm::StringRef File) override {
registerPragmaHandler(CI.getPreprocessor(), Pragmas);
return SyntaxOnlyAction::CreateASTConsumer(CI, File);
}
};
return std::make_unique<Action>(Pragmas);
};
}
// Populates AST
void parse() {
Pragmas = NullabilityPragmas{};
AST.emplace(Inputs);
}
FileID file(llvm::StringRef Path) {
auto File = AST->fileManager().getFileRef(Path);
if (!File) {
ADD_FAILURE() << llvm::toString(File.takeError());
return FileID();
}
FileID ID = AST->sourceManager().translateFile(*File);
EXPECT_TRUE(ID.isValid()) << Path;
return ID;
}
};
TEST_F(PragmaTest, None) {
Inputs.Code = "";
parse();
EXPECT_THAT(Pragmas, IsEmpty());
}
TEST_F(PragmaTest, Files) {
Inputs.Code = R"cpp(
#include "header.h"
#pragma nullability file_default nonnull
)cpp";
Inputs.ExtraFiles["header.h"] = "#pragma nullability file_default nullable";
parse();
EXPECT_THAT(Pragmas, UnorderedElementsAre(
Pair(file("main.cc"), NullabilityKind::NonNull),
Pair(file("header.h"), NullabilityKind::Nullable)));
}
TEST_F(PragmaTest, Macro) {
Inputs.Code = R"cpp(
#include "header.h"
DEFAULT_NONNULL
)cpp";
Inputs.ExtraFiles["header.h"] = R"cpp(
#define DEFAULT_NONNULL _Pragma("nullability file_default nonnull")
)cpp";
parse();
EXPECT_THAT(Pragmas, UnorderedElementsAre(
Pair(file("main.cc"), NullabilityKind::NonNull)));
}
MATCHER_P(message, M, "") { return arg.getMessage() == M; }
TEST_F(PragmaTest, Invalid) {
Inputs.Code = R"cpp(
#pragma nullability file_default nonnull
#pragma nullability file_default nullable
#pragma nullability file_default bleh
)cpp";
parse();
EXPECT_THAT(Pragmas, UnorderedElementsAre(
Pair(file("main.cc"), NullabilityKind::NonNull)));
EXPECT_THAT(
AST->diagnostics(),
ElementsAre(
message(
"ignoring repeated #pragma nullability file_default directive"),
message(
"ignoring invalid #pragma nullability file_default directive")));
}
} // namespace
} // namespace clang::tidy::nullability