2013-07-29 16:19:24 +08:00
|
|
|
#include "ClangTidyTest.h"
|
2014-08-13 21:57:57 +08:00
|
|
|
#include "llvm/HeaderGuardCheck.h"
|
2014-05-20 00:39:08 +08:00
|
|
|
#include "llvm/IncludeOrderCheck.h"
|
2014-02-27 22:28:02 +08:00
|
|
|
#include "gtest/gtest.h"
|
2013-07-29 16:19:24 +08:00
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace tidy {
|
2014-02-27 22:28:02 +08:00
|
|
|
namespace test {
|
2013-07-29 16:19:24 +08:00
|
|
|
|
2014-08-14 06:12:38 +08:00
|
|
|
// FIXME: It seems this might be incompatible to dos path. Investigating.
|
|
|
|
#if !defined(_WIN32)
|
2014-09-17 01:41:19 +08:00
|
|
|
static std::string runHeaderGuardCheck(StringRef Code, const Twine &Filename,
|
2014-09-17 17:05:07 +08:00
|
|
|
unsigned ExpectedWarnings) {
|
2014-09-17 01:41:19 +08:00
|
|
|
std::vector<ClangTidyError> Errors;
|
|
|
|
std::string Result = test::runCheckOnCode<LLVMHeaderGuardCheck>(
|
|
|
|
Code, &Errors, Filename, std::string("-xc++-header"));
|
2014-09-17 17:05:07 +08:00
|
|
|
return Errors.size() == ExpectedWarnings ? Result : "invalid error count";
|
2014-08-13 21:57:57 +08:00
|
|
|
}
|
|
|
|
|
2014-08-21 00:01:42 +08:00
|
|
|
namespace {
|
|
|
|
struct WithEndifComment : public LLVMHeaderGuardCheck {
|
2014-09-12 16:53:36 +08:00
|
|
|
WithEndifComment(StringRef Name, ClangTidyContext *Context)
|
|
|
|
: LLVMHeaderGuardCheck(Name, Context) {}
|
2014-08-21 00:01:42 +08:00
|
|
|
bool shouldSuggestEndifComment(StringRef Filename) override { return true; }
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
static std::string runHeaderGuardCheckWithEndif(StringRef Code,
|
2014-09-17 01:41:19 +08:00
|
|
|
const Twine &Filename,
|
2014-09-17 17:05:07 +08:00
|
|
|
unsigned ExpectedWarnings) {
|
2014-09-17 01:41:19 +08:00
|
|
|
std::vector<ClangTidyError> Errors;
|
|
|
|
std::string Result = test::runCheckOnCode<WithEndifComment>(
|
|
|
|
Code, &Errors, Filename, std::string("-xc++-header"));
|
2014-09-17 17:05:07 +08:00
|
|
|
return Errors.size() == ExpectedWarnings ? Result : "invalid error count";
|
2014-08-21 00:01:42 +08:00
|
|
|
}
|
|
|
|
|
2014-08-13 21:57:57 +08:00
|
|
|
TEST(LLVMHeaderGuardCheckTest, FixHeaderGuards) {
|
|
|
|
EXPECT_EQ("#ifndef LLVM_ADT_FOO_H\n#define LLVM_ADT_FOO_H\n#endif\n",
|
|
|
|
runHeaderGuardCheck("#ifndef FOO\n#define FOO\n#endif\n",
|
2014-09-17 17:05:07 +08:00
|
|
|
"include/llvm/ADT/foo.h",
|
|
|
|
/*ExpectedWarnings=*/1));
|
2014-08-13 21:57:57 +08:00
|
|
|
|
|
|
|
// Allow trailing underscores.
|
|
|
|
EXPECT_EQ("#ifndef LLVM_ADT_FOO_H_\n#define LLVM_ADT_FOO_H_\n#endif\n",
|
|
|
|
runHeaderGuardCheck(
|
|
|
|
"#ifndef LLVM_ADT_FOO_H_\n#define LLVM_ADT_FOO_H_\n#endif\n",
|
2014-09-17 17:05:07 +08:00
|
|
|
"include/llvm/ADT/foo.h", /*ExpectedWarnings=*/0));
|
2014-08-13 21:57:57 +08:00
|
|
|
|
2014-09-17 17:05:07 +08:00
|
|
|
EXPECT_EQ(
|
|
|
|
"#ifndef LLVM_CLANG_C_BAR_H\n#define LLVM_CLANG_C_BAR_H\n\n\n#endif\n",
|
|
|
|
runHeaderGuardCheck("", "./include/clang-c/bar.h",
|
|
|
|
/*ExpectedWarnings=*/1));
|
2014-08-13 21:57:57 +08:00
|
|
|
|
|
|
|
EXPECT_EQ("#ifndef LLVM_CLANG_LIB_CODEGEN_C_H\n#define "
|
|
|
|
"LLVM_CLANG_LIB_CODEGEN_C_H\n\n\n#endif\n",
|
2014-09-17 17:05:07 +08:00
|
|
|
runHeaderGuardCheck("", "tools/clang/lib/CodeGen/c.h",
|
|
|
|
/*ExpectedWarnings=*/1));
|
2014-08-13 21:57:57 +08:00
|
|
|
|
|
|
|
EXPECT_EQ("#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_X_H\n#define "
|
|
|
|
"LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_X_H\n\n\n#endif\n",
|
2014-09-17 17:05:07 +08:00
|
|
|
runHeaderGuardCheck("", "tools/clang/tools/extra/clang-tidy/x.h",
|
|
|
|
/*ExpectedWarnings=*/1));
|
2014-08-13 21:57:57 +08:00
|
|
|
|
|
|
|
EXPECT_EQ(
|
|
|
|
"int foo;\n#ifndef LLVM_CLANG_BAR_H\n#define LLVM_CLANG_BAR_H\n#endif\n",
|
|
|
|
runHeaderGuardCheck("int foo;\n#ifndef LLVM_CLANG_BAR_H\n"
|
|
|
|
"#define LLVM_CLANG_BAR_H\n#endif\n",
|
2014-09-17 17:05:07 +08:00
|
|
|
"include/clang/bar.h", /*ExpectedWarnings=*/1));
|
2014-08-13 21:57:57 +08:00
|
|
|
|
|
|
|
EXPECT_EQ("#ifndef LLVM_CLANG_BAR_H\n#define LLVM_CLANG_BAR_H\n\n"
|
|
|
|
"int foo;\n#ifndef FOOLOLO\n#define FOOLOLO\n#endif\n\n#endif\n",
|
|
|
|
runHeaderGuardCheck(
|
|
|
|
"int foo;\n#ifndef FOOLOLO\n#define FOOLOLO\n#endif\n",
|
2014-09-17 17:05:07 +08:00
|
|
|
"include/clang/bar.h", /*ExpectedWarnings=*/1));
|
2014-08-21 00:01:42 +08:00
|
|
|
|
|
|
|
EXPECT_EQ("#ifndef LLVM_ADT_FOO_H\n#define LLVM_ADT_FOO_H\n#endif "
|
|
|
|
" // LLVM_ADT_FOO_H\n",
|
|
|
|
runHeaderGuardCheckWithEndif("#ifndef FOO\n#define FOO\n#endif\n",
|
2014-09-17 17:05:07 +08:00
|
|
|
"include/llvm/ADT/foo.h",
|
|
|
|
/*ExpectedWarnings=*/1));
|
2014-08-21 00:01:42 +08:00
|
|
|
|
|
|
|
EXPECT_EQ("#ifndef LLVM_ADT_FOO_H\n#define LLVM_ADT_FOO_H\n#endif "
|
|
|
|
" // LLVM_ADT_FOO_H\n",
|
|
|
|
runHeaderGuardCheckWithEndif("#ifndef LLVM_ADT_FOO_H\n#define "
|
|
|
|
"LLVM_ADT_FOO_H\n#endif // LLVM_H\n",
|
2014-09-17 17:05:07 +08:00
|
|
|
"include/llvm/ADT/foo.h",
|
|
|
|
/*ExpectedWarnings=*/1));
|
2014-08-21 00:01:42 +08:00
|
|
|
|
|
|
|
EXPECT_EQ("#ifndef LLVM_ADT_FOO_H\n#define LLVM_ADT_FOO_H\n#endif"
|
|
|
|
" /* LLVM_ADT_FOO_H */\n",
|
|
|
|
runHeaderGuardCheckWithEndif("#ifndef LLVM_ADT_FOO_H\n#define "
|
|
|
|
"LLVM_ADT_FOO_H\n"
|
|
|
|
"#endif /* LLVM_ADT_FOO_H */\n",
|
2014-09-17 17:05:07 +08:00
|
|
|
"include/llvm/ADT/foo.h",
|
|
|
|
/*ExpectedWarnings=*/0));
|
2014-08-27 00:28:35 +08:00
|
|
|
|
|
|
|
EXPECT_EQ("#ifndef LLVM_ADT_FOO_H_\n#define LLVM_ADT_FOO_H_\n#endif "
|
|
|
|
"// LLVM_ADT_FOO_H_\n",
|
|
|
|
runHeaderGuardCheckWithEndif(
|
|
|
|
"#ifndef LLVM_ADT_FOO_H_\n#define "
|
|
|
|
"LLVM_ADT_FOO_H_\n#endif // LLVM_ADT_FOO_H_\n",
|
2014-09-17 17:05:07 +08:00
|
|
|
"include/llvm/ADT/foo.h", /*ExpectedWarnings=*/0));
|
2014-08-27 00:28:35 +08:00
|
|
|
|
|
|
|
EXPECT_EQ(
|
|
|
|
"#ifndef LLVM_ADT_FOO_H\n#define LLVM_ADT_FOO_H\n#endif // "
|
|
|
|
"LLVM_ADT_FOO_H\n",
|
|
|
|
runHeaderGuardCheckWithEndif(
|
|
|
|
"#ifndef LLVM_ADT_FOO_H_\n#define LLVM_ADT_FOO_H_\n#endif // LLVM\n",
|
2014-09-17 17:05:07 +08:00
|
|
|
"include/llvm/ADT/foo.h", /*ExpectedWarnings=*/1));
|
2014-09-10 16:48:30 +08:00
|
|
|
|
|
|
|
EXPECT_EQ("#ifndef LLVM_ADT_FOO_H\n#define LLVM_ADT_FOO_H\n#endif \\ \n// "
|
|
|
|
"LLVM_ADT_FOO_H\n",
|
|
|
|
runHeaderGuardCheckWithEndif("#ifndef LLVM_ADT_FOO_H\n#define "
|
|
|
|
"LLVM_ADT_FOO_H\n#endif \\ \n// "
|
|
|
|
"LLVM_ADT_FOO_H\n",
|
2014-09-17 17:05:07 +08:00
|
|
|
"include/llvm/ADT/foo.h",
|
2014-10-07 23:49:36 +08:00
|
|
|
/*ExpectedWarnings=*/1));
|
2014-09-10 16:48:30 +08:00
|
|
|
|
|
|
|
EXPECT_EQ("#ifndef LLVM_ADT_FOO_H\n#define LLVM_ADT_FOO_H\n#endif /* "
|
|
|
|
"LLVM_ADT_FOO_H\\ \n FOO */",
|
|
|
|
runHeaderGuardCheckWithEndif(
|
|
|
|
"#ifndef LLVM_ADT_FOO_H\n#define LLVM_ADT_FOO_H\n#endif /* "
|
|
|
|
"LLVM_ADT_FOO_H\\ \n FOO */",
|
2014-09-17 17:05:07 +08:00
|
|
|
"include/llvm/ADT/foo.h", /*ExpectedWarnings=*/0));
|
2014-08-13 21:57:57 +08:00
|
|
|
}
|
2014-08-14 06:12:38 +08:00
|
|
|
#endif
|
2014-08-13 21:57:57 +08:00
|
|
|
|
2014-02-27 22:28:02 +08:00
|
|
|
} // namespace test
|
2013-07-29 16:19:24 +08:00
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|