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