[clang-tools-extra] r216072 - [clang-tidy] Allow /**/ comments on #endifs when checking header guards.
Benjamin Kramer
benny.kra at googlemail.com
Wed Aug 20 09:01:43 PDT 2014
Author: d0k
Date: Wed Aug 20 11:01:42 2014
New Revision: 216072
URL: http://llvm.org/viewvc/llvm-project?rev=216072&view=rev
Log:
[clang-tidy] Allow /**/ comments on #endifs when checking header guards.
Turning block comments into line comments just creates unecessary churn.
Modified:
clang-tools-extra/trunk/clang-tidy/utils/HeaderGuard.cpp
clang-tools-extra/trunk/unittests/clang-tidy/LLVMModuleTest.cpp
Modified: clang-tools-extra/trunk/clang-tidy/utils/HeaderGuard.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/utils/HeaderGuard.cpp?rev=216072&r1=216071&r2=216072&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/utils/HeaderGuard.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/utils/HeaderGuard.cpp Wed Aug 20 11:01:42 2014
@@ -164,7 +164,8 @@ public:
size_t EndIfLen = std::strcspn(EndIfData, "\r\n");
StringRef EndIfStr(EndIfData, EndIfLen);
- if (EndIf.isValid() && !EndIfStr.endswith("// " + HeaderGuard.str())) {
+ if (EndIf.isValid() && !EndIfStr.endswith("// " + HeaderGuard.str()) &&
+ !EndIfStr.endswith("/* " + HeaderGuard.str() + " */")) {
std::string Correct = "endif // " + HeaderGuard.str();
Check->diag(EndIf, "#endif for a header guard should reference the "
"guard macro in a comment")
Modified: clang-tools-extra/trunk/unittests/clang-tidy/LLVMModuleTest.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/unittests/clang-tidy/LLVMModuleTest.cpp?rev=216072&r1=216071&r2=216072&view=diff
==============================================================================
--- clang-tools-extra/trunk/unittests/clang-tidy/LLVMModuleTest.cpp (original)
+++ clang-tools-extra/trunk/unittests/clang-tidy/LLVMModuleTest.cpp Wed Aug 20 11:01:42 2014
@@ -93,6 +93,18 @@ static std::string runHeaderGuardCheck(S
Code, /*Errors=*/nullptr, Filename, std::string("-xc++-header"));
}
+namespace {
+struct WithEndifComment : public LLVMHeaderGuardCheck {
+ bool shouldSuggestEndifComment(StringRef Filename) override { return true; }
+};
+} // namespace
+
+static std::string runHeaderGuardCheckWithEndif(StringRef Code,
+ const Twine &Filename) {
+ return test::runCheckOnCode<WithEndifComment>(
+ Code, /*Errors=*/nullptr, Filename, std::string("-xc++-header"));
+}
+
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",
@@ -126,6 +138,24 @@ TEST(LLVMHeaderGuardCheckTest, FixHeader
runHeaderGuardCheck(
"int foo;\n#ifndef FOOLOLO\n#define FOOLOLO\n#endif\n",
"include/clang/bar.h"));
+
+ 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",
+ "include/llvm/ADT/foo.h"));
+
+ 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",
+ "include/llvm/ADT/foo.h"));
+
+ 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",
+ "include/llvm/ADT/foo.h"));
}
#endif
More information about the cfe-commits
mailing list