1 #include "ClangTidyTest.h" 2 #include "llvm/HeaderGuardCheck.h" 3 #include "llvm/IncludeOrderCheck.h" 4 #include "gtest/gtest.h" 5 6 namespace clang { 7 namespace tidy { 8 namespace test { 9 10 // FIXME: It seems this might be incompatible to dos path. Investigating. 11 #if !defined(_WIN32) 12 static std::string runHeaderGuardCheck(StringRef Code, const Twine &Filename, 13 unsigned ExpectedWarnings) { 14 std::vector<ClangTidyError> Errors; 15 std::string Result = test::runCheckOnCode<LLVMHeaderGuardCheck>( 16 Code, &Errors, Filename, std::string("-xc++-header")); 17 return Errors.size() == ExpectedWarnings ? Result : "invalid error count"; 18 } 19 20 namespace { 21 struct WithEndifComment : public LLVMHeaderGuardCheck { 22 WithEndifComment(StringRef Name, ClangTidyContext *Context) 23 : LLVMHeaderGuardCheck(Name, Context) {} 24 bool shouldSuggestEndifComment(StringRef Filename) override { return true; } 25 }; 26 } // namespace 27 28 static std::string runHeaderGuardCheckWithEndif(StringRef Code, 29 const Twine &Filename, 30 unsigned ExpectedWarnings) { 31 std::vector<ClangTidyError> Errors; 32 std::string Result = test::runCheckOnCode<WithEndifComment>( 33 Code, &Errors, Filename, std::string("-xc++-header")); 34 return Errors.size() == ExpectedWarnings ? Result : "invalid error count"; 35 } 36 37 TEST(LLVMHeaderGuardCheckTest, FixHeaderGuards) { 38 EXPECT_EQ("#ifndef LLVM_ADT_FOO_H\n#define LLVM_ADT_FOO_H\n#endif\n", 39 runHeaderGuardCheck("#ifndef FOO\n#define FOO\n#endif\n", 40 "include/llvm/ADT/foo.h", 41 /*ExpectedWarnings=*/1)); 42 43 // Allow trailing underscores. 44 EXPECT_EQ("#ifndef LLVM_ADT_FOO_H_\n#define LLVM_ADT_FOO_H_\n#endif\n", 45 runHeaderGuardCheck( 46 "#ifndef LLVM_ADT_FOO_H_\n#define LLVM_ADT_FOO_H_\n#endif\n", 47 "include/llvm/ADT/foo.h", /*ExpectedWarnings=*/0)); 48 49 EXPECT_EQ( 50 "#ifndef LLVM_CLANG_C_BAR_H\n#define LLVM_CLANG_C_BAR_H\n\n\n#endif\n", 51 runHeaderGuardCheck("", "./include/clang-c/bar.h", 52 /*ExpectedWarnings=*/1)); 53 54 EXPECT_EQ("#ifndef LLVM_CLANG_LIB_CODEGEN_C_H\n#define " 55 "LLVM_CLANG_LIB_CODEGEN_C_H\n\n\n#endif\n", 56 runHeaderGuardCheck("", "tools/clang/lib/CodeGen/c.h", 57 /*ExpectedWarnings=*/1)); 58 59 EXPECT_EQ("#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_X_H\n#define " 60 "LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_X_H\n\n\n#endif\n", 61 runHeaderGuardCheck("", "tools/clang/tools/extra/clang-tidy/x.h", 62 /*ExpectedWarnings=*/1)); 63 64 EXPECT_EQ( 65 "int foo;\n#ifndef LLVM_CLANG_BAR_H\n#define LLVM_CLANG_BAR_H\n#endif\n", 66 runHeaderGuardCheck("int foo;\n#ifndef LLVM_CLANG_BAR_H\n" 67 "#define LLVM_CLANG_BAR_H\n#endif\n", 68 "include/clang/bar.h", /*ExpectedWarnings=*/1)); 69 70 EXPECT_EQ("#ifndef LLVM_CLANG_BAR_H\n#define LLVM_CLANG_BAR_H\n\n" 71 "int foo;\n#ifndef FOOLOLO\n#define FOOLOLO\n#endif\n\n#endif\n", 72 runHeaderGuardCheck( 73 "int foo;\n#ifndef FOOLOLO\n#define FOOLOLO\n#endif\n", 74 "include/clang/bar.h", /*ExpectedWarnings=*/1)); 75 76 EXPECT_EQ("#ifndef LLVM_ADT_FOO_H\n#define LLVM_ADT_FOO_H\n#endif " 77 " // LLVM_ADT_FOO_H\n", 78 runHeaderGuardCheckWithEndif("#ifndef FOO\n#define FOO\n#endif\n", 79 "include/llvm/ADT/foo.h", 80 /*ExpectedWarnings=*/1)); 81 82 EXPECT_EQ("#ifndef LLVM_ADT_FOO_H\n#define LLVM_ADT_FOO_H\n#endif " 83 " // LLVM_ADT_FOO_H\n", 84 runHeaderGuardCheckWithEndif("#ifndef LLVM_ADT_FOO_H\n#define " 85 "LLVM_ADT_FOO_H\n#endif // LLVM_H\n", 86 "include/llvm/ADT/foo.h", 87 /*ExpectedWarnings=*/1)); 88 89 EXPECT_EQ("#ifndef LLVM_ADT_FOO_H\n#define LLVM_ADT_FOO_H\n#endif" 90 " /* LLVM_ADT_FOO_H */\n", 91 runHeaderGuardCheckWithEndif("#ifndef LLVM_ADT_FOO_H\n#define " 92 "LLVM_ADT_FOO_H\n" 93 "#endif /* LLVM_ADT_FOO_H */\n", 94 "include/llvm/ADT/foo.h", 95 /*ExpectedWarnings=*/0)); 96 97 EXPECT_EQ("#ifndef LLVM_ADT_FOO_H_\n#define LLVM_ADT_FOO_H_\n#endif " 98 "// LLVM_ADT_FOO_H_\n", 99 runHeaderGuardCheckWithEndif( 100 "#ifndef LLVM_ADT_FOO_H_\n#define " 101 "LLVM_ADT_FOO_H_\n#endif // LLVM_ADT_FOO_H_\n", 102 "include/llvm/ADT/foo.h", /*ExpectedWarnings=*/0)); 103 104 EXPECT_EQ( 105 "#ifndef LLVM_ADT_FOO_H\n#define LLVM_ADT_FOO_H\n#endif // " 106 "LLVM_ADT_FOO_H\n", 107 runHeaderGuardCheckWithEndif( 108 "#ifndef LLVM_ADT_FOO_H_\n#define LLVM_ADT_FOO_H_\n#endif // LLVM\n", 109 "include/llvm/ADT/foo.h", /*ExpectedWarnings=*/1)); 110 111 EXPECT_EQ("#ifndef LLVM_ADT_FOO_H\n#define LLVM_ADT_FOO_H\n#endif \\ \n// " 112 "LLVM_ADT_FOO_H\n", 113 runHeaderGuardCheckWithEndif("#ifndef LLVM_ADT_FOO_H\n#define " 114 "LLVM_ADT_FOO_H\n#endif \\ \n// " 115 "LLVM_ADT_FOO_H\n", 116 "include/llvm/ADT/foo.h", 117 /*ExpectedWarnings=*/0)); 118 119 EXPECT_EQ("#ifndef LLVM_ADT_FOO_H\n#define LLVM_ADT_FOO_H\n#endif /* " 120 "LLVM_ADT_FOO_H\\ \n FOO */", 121 runHeaderGuardCheckWithEndif( 122 "#ifndef LLVM_ADT_FOO_H\n#define LLVM_ADT_FOO_H\n#endif /* " 123 "LLVM_ADT_FOO_H\\ \n FOO */", 124 "include/llvm/ADT/foo.h", /*ExpectedWarnings=*/0)); 125 } 126 #endif 127 128 } // namespace test 129 } // namespace tidy 130 } // namespace clang 131