xref: /llvm-project/clang-tools-extra/unittests/clang-tidy/LLVMModuleTest.cpp (revision 0a6ce9f4e1429b001b6fb5feabc60d23cc0e5ef8)
1 #include "ClangTidyTest.h"
2 #include "llvm/HeaderGuardCheck.h"
3 #include "llvm/IncludeOrderCheck.h"
4 #include "gtest/gtest.h"
5 
6 using namespace clang::tidy::llvm;
7 
8 namespace clang {
9 namespace tidy {
10 namespace test {
11 
12 // FIXME: It seems this might be incompatible to dos path. Investigating.
13 #if !defined(_WIN32)
14 static std::string runHeaderGuardCheck(StringRef Code, const Twine &Filename,
15                                        unsigned ExpectedWarnings) {
16   std::vector<ClangTidyError> Errors;
17   std::string Result = test::runCheckOnCode<LLVMHeaderGuardCheck>(
18       Code, &Errors, Filename, std::string("-xc++-header"));
19   return Errors.size() == ExpectedWarnings ? Result : "invalid error count";
20 }
21 
22 namespace {
23 struct WithEndifComment : public LLVMHeaderGuardCheck {
24   WithEndifComment(StringRef Name, ClangTidyContext *Context)
25       : LLVMHeaderGuardCheck(Name, Context) {}
26   bool shouldSuggestEndifComment(StringRef Filename) override { return true; }
27 };
28 } // namespace
29 
30 static std::string runHeaderGuardCheckWithEndif(StringRef Code,
31                                                 const Twine &Filename,
32                                                 unsigned ExpectedWarnings) {
33   std::vector<ClangTidyError> Errors;
34   std::string Result = test::runCheckOnCode<WithEndifComment>(
35       Code, &Errors, Filename, std::string("-xc++-header"));
36   return Errors.size() == ExpectedWarnings ? Result : "invalid error count";
37 }
38 
39 TEST(LLVMHeaderGuardCheckTest, FixHeaderGuards) {
40   EXPECT_EQ("#ifndef LLVM_ADT_FOO_H\n"
41             "#define LLVM_ADT_FOO_H\n"
42             "#endif\n",
43             runHeaderGuardCheck("#ifndef FOO\n"
44                                 "#define FOO\n"
45                                 "#endif\n",
46                                 "include/llvm/ADT/foo.h",
47                                 /*ExpectedWarnings=*/1));
48 
49   // Allow trailing underscores.
50   EXPECT_EQ("#ifndef LLVM_ADT_FOO_H_\n"
51             "#define LLVM_ADT_FOO_H_\n"
52             "#endif\n",
53             runHeaderGuardCheck("#ifndef LLVM_ADT_FOO_H_\n"
54                                 "#define LLVM_ADT_FOO_H_\n"
55                                 "#endif\n",
56                                 "include/llvm/ADT/foo.h",
57                                 /*ExpectedWarnings=*/0));
58 
59   EXPECT_EQ("#ifndef LLVM_CLANG_C_BAR_H\n"
60             "#define LLVM_CLANG_C_BAR_H\n"
61             "\n"
62             "\n"
63             "#endif\n",
64             runHeaderGuardCheck("", "./include/clang-c/bar.h",
65                                 /*ExpectedWarnings=*/1));
66 
67   EXPECT_EQ("#ifndef LLVM_CLANG_LIB_CODEGEN_C_H\n"
68             "#define LLVM_CLANG_LIB_CODEGEN_C_H\n"
69             "\n"
70             "\n"
71             "#endif\n",
72             runHeaderGuardCheck("", "tools/clang/lib/CodeGen/c.h",
73                                 /*ExpectedWarnings=*/1));
74 
75   EXPECT_EQ("#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_X_H\n"
76             "#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_X_H\n"
77             "\n"
78             "\n"
79             "#endif\n",
80             runHeaderGuardCheck("", "tools/clang/tools/extra/clang-tidy/x.h",
81                                 /*ExpectedWarnings=*/1));
82 
83   EXPECT_EQ("int foo;\n"
84             "#ifndef LLVM_CLANG_BAR_H\n"
85             "#define LLVM_CLANG_BAR_H\n"
86             "#endif\n",
87             runHeaderGuardCheck("int foo;\n"
88                                 "#ifndef LLVM_CLANG_BAR_H\n"
89                                 "#define LLVM_CLANG_BAR_H\n"
90                                 "#endif\n",
91                                 "include/clang/bar.h", /*ExpectedWarnings=*/1));
92 
93   EXPECT_EQ("#ifndef LLVM_CLANG_BAR_H\n"
94             "#define LLVM_CLANG_BAR_H\n"
95             "\n"
96             "int foo;\n"
97             "#ifndef FOOLOLO\n"
98             "#define FOOLOLO\n"
99             "#endif\n"
100             "\n"
101             "#endif\n",
102             runHeaderGuardCheck("int foo;\n"
103                                 "#ifndef FOOLOLO\n"
104                                 "#define FOOLOLO\n"
105                                 "#endif\n",
106                                 "include/clang/bar.h", /*ExpectedWarnings=*/1));
107 
108   // Fix incorrect #endif comments even if we shouldn't add new ones.
109   EXPECT_EQ("#ifndef LLVM_ADT_FOO_H\n"
110             "#define LLVM_ADT_FOO_H\n"
111             "#endif // LLVM_ADT_FOO_H\n",
112             runHeaderGuardCheck("#ifndef FOO\n"
113                                 "#define FOO\n"
114                                 "#endif // FOO\n",
115                                 "include/llvm/ADT/foo.h",
116                                 /*ExpectedWarnings=*/1));
117 
118   EXPECT_EQ("#ifndef LLVM_ADT_FOO_H\n"
119             "#define LLVM_ADT_FOO_H\n"
120             "#endif // LLVM_ADT_FOO_H\n",
121             runHeaderGuardCheckWithEndif("#ifndef FOO\n"
122                                          "#define FOO\n"
123                                          "#endif\n",
124                                          "include/llvm/ADT/foo.h",
125                                          /*ExpectedWarnings=*/1));
126 
127   EXPECT_EQ("#ifndef LLVM_ADT_FOO_H\n"
128             "#define LLVM_ADT_FOO_H\n"
129             "#endif // LLVM_ADT_FOO_H\n",
130             runHeaderGuardCheckWithEndif("#ifndef LLVM_ADT_FOO_H\n"
131                                          "#define LLVM_ADT_FOO_H\n"
132                                          "#endif // LLVM_H\n",
133                                          "include/llvm/ADT/foo.h",
134                                          /*ExpectedWarnings=*/1));
135 
136   EXPECT_EQ("#ifndef LLVM_ADT_FOO_H\n"
137             "#define LLVM_ADT_FOO_H\n"
138             "#endif /* LLVM_ADT_FOO_H */\n",
139             runHeaderGuardCheckWithEndif("#ifndef LLVM_ADT_FOO_H\n"
140                                          "#define LLVM_ADT_FOO_H\n"
141                                          "#endif /* LLVM_ADT_FOO_H */\n",
142                                          "include/llvm/ADT/foo.h",
143                                          /*ExpectedWarnings=*/0));
144 
145   EXPECT_EQ("#ifndef LLVM_ADT_FOO_H_\n"
146             "#define LLVM_ADT_FOO_H_\n"
147             "#endif // LLVM_ADT_FOO_H_\n",
148             runHeaderGuardCheckWithEndif("#ifndef LLVM_ADT_FOO_H_\n"
149                                          "#define LLVM_ADT_FOO_H_\n"
150                                          "#endif // LLVM_ADT_FOO_H_\n",
151                                          "include/llvm/ADT/foo.h",
152                                          /*ExpectedWarnings=*/0));
153 
154   EXPECT_EQ("#ifndef LLVM_ADT_FOO_H\n"
155             "#define LLVM_ADT_FOO_H\n"
156             "#endif // LLVM_ADT_FOO_H\n",
157             runHeaderGuardCheckWithEndif("#ifndef LLVM_ADT_FOO_H_\n"
158                                          "#define LLVM_ADT_FOO_H_\n"
159                                          "#endif // LLVM\n",
160                                          "include/llvm/ADT/foo.h",
161                                          /*ExpectedWarnings=*/1));
162 
163   EXPECT_EQ("#ifndef LLVM_ADT_FOO_H\n"
164             "#define LLVM_ADT_FOO_H\n"
165             "#endif \\ \n"
166             "// LLVM_ADT_FOO_H\n",
167             runHeaderGuardCheckWithEndif("#ifndef LLVM_ADT_FOO_H\n"
168                                          "#define LLVM_ADT_FOO_H\n"
169                                          "#endif \\ \n"
170                                          "// LLVM_ADT_FOO_H\n",
171                                          "include/llvm/ADT/foo.h",
172                                          /*ExpectedWarnings=*/1));
173 
174   EXPECT_EQ("#ifndef LLVM_ADT_FOO_H\n"
175             "#define LLVM_ADT_FOO_H\n"
176             "#endif  /* LLVM_ADT_FOO_H\\ \n"
177             " FOO */",
178             runHeaderGuardCheckWithEndif("#ifndef LLVM_ADT_FOO_H\n"
179                                          "#define LLVM_ADT_FOO_H\n"
180                                          "#endif  /* LLVM_ADT_FOO_H\\ \n"
181                                          " FOO */",
182                                          "include/llvm/ADT/foo.h",
183                                          /*ExpectedWarnings=*/0));
184 }
185 #endif
186 
187 } // namespace test
188 } // namespace tidy
189 } // namespace clang
190