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
|
|
|
|
2019-05-11 02:27:09 +08:00
|
|
|
using namespace clang::tidy::llvm_check;
|
2015-03-02 20:39:18 +08:00
|
|
|
|
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,
|
2017-02-21 19:25:45 +08:00
|
|
|
Optional<StringRef> ExpectedWarning) {
|
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"));
|
2017-02-21 19:25:45 +08:00
|
|
|
if (Errors.size() != (size_t)ExpectedWarning.hasValue())
|
|
|
|
return "invalid error count";
|
|
|
|
if (ExpectedWarning && *ExpectedWarning != Errors.back().Message.Message)
|
|
|
|
return "expected: '" + ExpectedWarning->str() + "', saw: '" +
|
|
|
|
Errors.back().Message.Message + "'";
|
|
|
|
return Result;
|
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
|
|
|
|
|
2017-02-21 19:25:45 +08:00
|
|
|
static std::string
|
|
|
|
runHeaderGuardCheckWithEndif(StringRef Code, const Twine &Filename,
|
|
|
|
Optional<StringRef> ExpectedWarning) {
|
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"));
|
2017-02-21 19:25:45 +08:00
|
|
|
if (Errors.size() != (size_t)ExpectedWarning.hasValue())
|
|
|
|
return "invalid error count";
|
|
|
|
if (ExpectedWarning && *ExpectedWarning != Errors.back().Message.Message)
|
|
|
|
return "expected: '" + ExpectedWarning->str() + "', saw: '" +
|
|
|
|
Errors.back().Message.Message + "'";
|
|
|
|
return Result;
|
2014-08-21 00:01:42 +08:00
|
|
|
}
|
|
|
|
|
2014-08-13 21:57:57 +08:00
|
|
|
TEST(LLVMHeaderGuardCheckTest, FixHeaderGuards) {
|
2014-10-15 19:36:48 +08:00
|
|
|
EXPECT_EQ("#ifndef LLVM_ADT_FOO_H\n"
|
|
|
|
"#define LLVM_ADT_FOO_H\n"
|
|
|
|
"#endif\n",
|
2017-02-21 19:25:45 +08:00
|
|
|
runHeaderGuardCheck(
|
|
|
|
"#ifndef FOO\n"
|
|
|
|
"#define FOO\n"
|
|
|
|
"#endif\n",
|
|
|
|
"include/llvm/ADT/foo.h",
|
|
|
|
StringRef("header guard does not follow preferred style")));
|
2014-08-13 21:57:57 +08:00
|
|
|
|
|
|
|
// Allow trailing underscores.
|
2014-10-15 19:36:48 +08:00
|
|
|
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",
|
2017-02-21 19:25:45 +08:00
|
|
|
"include/llvm/ADT/foo.h", None));
|
2014-10-15 19:36:48 +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",
|
2017-02-21 19:25:45 +08:00
|
|
|
StringRef("header is missing header guard")));
|
2014-10-15 19:36:48 +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",
|
2017-02-21 19:25:45 +08:00
|
|
|
StringRef("header is missing header guard")));
|
2014-08-13 21:57:57 +08:00
|
|
|
|
2014-10-15 19:36:48 +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",
|
2017-02-21 19:25:45 +08:00
|
|
|
StringRef("header is missing header guard")));
|
|
|
|
|
|
|
|
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",
|
|
|
|
"include/clang/bar.h",
|
|
|
|
StringRef("code/includes outside of area guarded by "
|
|
|
|
"header guard; consider moving it")));
|
|
|
|
|
|
|
|
EXPECT_EQ(
|
|
|
|
"#ifndef LLVM_CLANG_BAR_H\n"
|
|
|
|
"#define LLVM_CLANG_BAR_H\n"
|
|
|
|
"#endif\n"
|
|
|
|
"int foo;\n",
|
|
|
|
runHeaderGuardCheck("#ifndef LLVM_CLANG_BAR_H\n"
|
|
|
|
"#define LLVM_CLANG_BAR_H\n"
|
|
|
|
"#endif\n"
|
|
|
|
"int foo;\n",
|
|
|
|
"include/clang/bar.h",
|
|
|
|
StringRef("code/includes outside of area guarded by "
|
|
|
|
"header guard; consider moving it")));
|
2014-10-15 19:36:48 +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",
|
2017-02-21 19:25:45 +08:00
|
|
|
"include/clang/bar.h",
|
|
|
|
StringRef("header is missing header guard")));
|
2014-10-15 19:36:48 +08:00
|
|
|
|
2014-10-15 20:18:35 +08:00
|
|
|
// Fix incorrect #endif comments even if we shouldn't add new ones.
|
2014-10-15 19:36:48 +08:00
|
|
|
EXPECT_EQ("#ifndef LLVM_ADT_FOO_H\n"
|
|
|
|
"#define LLVM_ADT_FOO_H\n"
|
2014-10-15 20:18:35 +08:00
|
|
|
"#endif // LLVM_ADT_FOO_H\n",
|
2017-02-21 19:25:45 +08:00
|
|
|
runHeaderGuardCheck(
|
|
|
|
"#ifndef FOO\n"
|
|
|
|
"#define FOO\n"
|
|
|
|
"#endif // FOO\n",
|
|
|
|
"include/llvm/ADT/foo.h",
|
|
|
|
StringRef("header guard does not follow preferred style")));
|
2014-10-15 20:18:35 +08:00
|
|
|
|
|
|
|
EXPECT_EQ("#ifndef LLVM_ADT_FOO_H\n"
|
|
|
|
"#define LLVM_ADT_FOO_H\n"
|
|
|
|
"#endif // LLVM_ADT_FOO_H\n",
|
2017-02-21 19:25:45 +08:00
|
|
|
runHeaderGuardCheckWithEndif(
|
|
|
|
"#ifndef FOO\n"
|
|
|
|
"#define FOO\n"
|
|
|
|
"#endif\n",
|
|
|
|
"include/llvm/ADT/foo.h",
|
|
|
|
StringRef("header guard does not follow preferred style")));
|
2014-08-21 00:01:42 +08:00
|
|
|
|
2014-10-15 19:36:48 +08:00
|
|
|
EXPECT_EQ("#ifndef LLVM_ADT_FOO_H\n"
|
|
|
|
"#define LLVM_ADT_FOO_H\n"
|
2014-10-15 20:18:35 +08:00
|
|
|
"#endif // LLVM_ADT_FOO_H\n",
|
2017-02-21 19:25:45 +08:00
|
|
|
runHeaderGuardCheckWithEndif(
|
|
|
|
"#ifndef LLVM_ADT_FOO_H\n"
|
|
|
|
"#define LLVM_ADT_FOO_H\n"
|
|
|
|
"#endif // LLVM_H\n",
|
|
|
|
"include/llvm/ADT/foo.h",
|
|
|
|
StringRef("#endif for a header guard should reference the "
|
|
|
|
"guard macro in a comment")));
|
2014-08-21 00:01:42 +08:00
|
|
|
|
2014-10-15 19:36:48 +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"
|
2014-08-21 00:01:42 +08:00
|
|
|
"#endif /* LLVM_ADT_FOO_H */\n",
|
2017-02-21 19:25:45 +08:00
|
|
|
"include/llvm/ADT/foo.h", None));
|
2014-08-27 00:28:35 +08:00
|
|
|
|
2014-10-15 19:36:48 +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",
|
2017-02-21 19:25:45 +08:00
|
|
|
"include/llvm/ADT/foo.h", None));
|
2014-10-15 19:36:48 +08:00
|
|
|
|
|
|
|
EXPECT_EQ("#ifndef LLVM_ADT_FOO_H\n"
|
|
|
|
"#define LLVM_ADT_FOO_H\n"
|
2014-10-15 20:18:35 +08:00
|
|
|
"#endif // LLVM_ADT_FOO_H\n",
|
2017-02-21 19:25:45 +08:00
|
|
|
runHeaderGuardCheckWithEndif(
|
|
|
|
"#ifndef LLVM_ADT_FOO_H_\n"
|
|
|
|
"#define LLVM_ADT_FOO_H_\n"
|
|
|
|
"#endif // LLVM\n",
|
|
|
|
"include/llvm/ADT/foo.h",
|
|
|
|
StringRef("header guard does not follow preferred style")));
|
2014-09-10 16:48:30 +08:00
|
|
|
|
2014-10-15 19:36:48 +08:00
|
|
|
EXPECT_EQ("#ifndef LLVM_ADT_FOO_H\n"
|
|
|
|
"#define LLVM_ADT_FOO_H\n"
|
|
|
|
"#endif \\ \n"
|
|
|
|
"// LLVM_ADT_FOO_H\n",
|
2017-02-21 19:25:45 +08:00
|
|
|
runHeaderGuardCheckWithEndif(
|
|
|
|
"#ifndef LLVM_ADT_FOO_H\n"
|
|
|
|
"#define LLVM_ADT_FOO_H\n"
|
|
|
|
"#endif \\ \n"
|
|
|
|
"// LLVM_ADT_FOO_H\n",
|
|
|
|
"include/llvm/ADT/foo.h",
|
|
|
|
StringRef("backslash and newline separated by space")));
|
2014-10-15 19:36:48 +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 */",
|
2017-02-21 19:25:45 +08:00
|
|
|
"include/llvm/ADT/foo.h", None));
|
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
|