1
+ #include " ClangTidyOptions.h"
1
2
#include " ClangTidyTest.h"
3
+ #include " llvm/ADT/ArrayRef.h"
4
+ #include " llvm/ADT/StringRef.h"
2
5
#include " llvm/HeaderGuardCheck.h"
3
6
#include " llvm/IncludeOrderCheck.h"
4
7
#include " gtest/gtest.h"
@@ -9,11 +12,15 @@ namespace clang {
9
12
namespace tidy {
10
13
namespace test {
11
14
12
- static std::string runHeaderGuardCheck (StringRef Code, const Twine &Filename,
13
- Optional<StringRef> ExpectedWarning) {
15
+ template <typename T>
16
+ static std::string runCheck (StringRef Code, const Twine &Filename,
17
+ Optional<StringRef> ExpectedWarning,
18
+ std::map<StringRef, StringRef> PathsToContent =
19
+ std::map<StringRef, StringRef>()) {
14
20
std::vector<ClangTidyError> Errors;
15
- std::string Result = test::runCheckOnCode<LLVMHeaderGuardCheck>(
16
- Code, &Errors, Filename, std::string (" -xc++-header" ));
21
+ std::string Result = test::runCheckOnCode<T>(
22
+ Code, &Errors, Filename, std::string (" -xc++-header" ), ClangTidyOptions{},
23
+ std::move (PathsToContent));
17
24
if (Errors.size () != (size_t )ExpectedWarning.hasValue ())
18
25
return " invalid error count" ;
19
26
if (ExpectedWarning && *ExpectedWarning != Errors.back ().Message .Message )
@@ -22,27 +29,36 @@ static std::string runHeaderGuardCheck(StringRef Code, const Twine &Filename,
22
29
return Result;
23
30
}
24
31
32
+ static std::string runHeaderGuardCheck (StringRef Code, const Twine &Filename,
33
+ Optional<StringRef> ExpectedWarning) {
34
+ return runCheck<LLVMHeaderGuardCheck>(Code, Filename,
35
+ std::move (ExpectedWarning));
36
+ }
37
+
38
+ static std::string
39
+ runIncludeOrderCheck (StringRef Code, const Twine &Filename,
40
+ Optional<StringRef> ExpectedWarning,
41
+ llvm::ArrayRef<llvm::StringLiteral> Includes) {
42
+ std::map<StringRef, StringRef> PathsToContent;
43
+ for (auto Include : Includes)
44
+ PathsToContent.emplace (Include, " " );
45
+ return runCheck<IncludeOrderCheck>(Code, Filename, std::move (ExpectedWarning),
46
+ PathsToContent);
47
+ }
48
+
25
49
namespace {
26
50
struct WithEndifComment : public LLVMHeaderGuardCheck {
27
51
WithEndifComment (StringRef Name, ClangTidyContext *Context)
28
52
: LLVMHeaderGuardCheck(Name, Context) {}
29
53
bool shouldSuggestEndifComment (StringRef Filename) override { return true ; }
30
54
};
31
- } // namespace
32
55
33
56
static std::string
34
57
runHeaderGuardCheckWithEndif (StringRef Code, const Twine &Filename,
35
58
Optional<StringRef> ExpectedWarning) {
36
- std::vector<ClangTidyError> Errors;
37
- std::string Result = test::runCheckOnCode<WithEndifComment>(
38
- Code, &Errors, Filename, std::string (" -xc++-header" ));
39
- if (Errors.size () != (size_t )ExpectedWarning.hasValue ())
40
- return " invalid error count" ;
41
- if (ExpectedWarning && *ExpectedWarning != Errors.back ().Message .Message )
42
- return " expected: '" + ExpectedWarning->str () + " ', saw: '" +
43
- Errors.back ().Message .Message + " '" ;
44
- return Result;
59
+ return runCheck<WithEndifComment>(Code, Filename, std::move (ExpectedWarning));
45
60
}
61
+ } // namespace
46
62
47
63
TEST (LLVMHeaderGuardCheckTest, FixHeaderGuards) {
48
64
EXPECT_EQ (" #ifndef LLVM_ADT_FOO_H\n "
@@ -270,6 +286,23 @@ TEST(LLVMHeaderGuardCheckTest, FixHeaderGuards) {
270
286
#endif
271
287
}
272
288
289
+ TEST (IncludeOrderCheck, GTestHeaders) {
290
+ EXPECT_EQ (
291
+ R"cpp(
292
+ #include "foo.h"
293
+ #include "llvm/foo.h"
294
+ #include "gtest/foo.h"
295
+ #include <algorithm>)cpp" ,
296
+ runIncludeOrderCheck (
297
+ R"cpp(
298
+ #include "foo.h"
299
+ #include "llvm/foo.h"
300
+ #include <algorithm>
301
+ #include "gtest/foo.h")cpp" ,
302
+ " foo.cc" , StringRef (" #includes are not sorted properly" ),
303
+ {" foo.h" , " algorithm" , " gtest/foo.h" , " llvm/foo.h" }));
304
+ }
305
+
273
306
} // namespace test
274
307
} // namespace tidy
275
308
} // namespace clang
0 commit comments