diff options
author | Alexander Kornienko <alexfh@google.com> | 2014-10-15 12:18:35 +0000 |
---|---|---|
committer | Alexander Kornienko <alexfh@google.com> | 2014-10-15 12:18:35 +0000 |
commit | 81d4f939bc031ee0710bc2d41d0e10648b4ff95b (patch) | |
tree | 94ba80ea7d35612ea1d1246e0447d6934a83ae36 /clang-tools-extra/clang-tidy/utils/HeaderGuard.cpp | |
parent | bebe0c6918e96f4f9e7fb21d6edf95e5753096aa (diff) | |
download | bcm5719-llvm-81d4f939bc031ee0710bc2d41d0e10648b4ff95b.tar.gz bcm5719-llvm-81d4f939bc031ee0710bc2d41d0e10648b4ff95b.zip |
Fix llvm-header-guard check.
Summary:
This patch makes the check work better for LLVM code:
* always fix existing #endif comments
* use one space before the comment (+allow customization for other styles)
Reviewers: djasper
Reviewed By: djasper
Subscribers: cfe-commits
Differential Revision: http://reviews.llvm.org/D5795
llvm-svn: 219789
Diffstat (limited to 'clang-tools-extra/clang-tidy/utils/HeaderGuard.cpp')
-rw-r--r-- | clang-tools-extra/clang-tidy/utils/HeaderGuard.cpp | 22 |
1 files changed, 15 insertions, 7 deletions
diff --git a/clang-tools-extra/clang-tidy/utils/HeaderGuard.cpp b/clang-tools-extra/clang-tidy/utils/HeaderGuard.cpp index 0a5fe988179..535dd5c8b45 100644 --- a/clang-tools-extra/clang-tidy/utils/HeaderGuard.cpp +++ b/clang-tools-extra/clang-tidy/utils/HeaderGuard.cpp @@ -150,15 +150,15 @@ public: bool wouldFixEndifComment(StringRef FileName, SourceLocation EndIf, StringRef HeaderGuard, size_t *EndIfLenPtr = nullptr) { - if (!Check->shouldSuggestEndifComment(FileName)) + if (!EndIf.isValid()) return false; - const char *EndIfData = PP->getSourceManager().getCharacterData(EndIf); size_t EndIfLen = std::strcspn(EndIfData, "\r\n"); if (EndIfLenPtr) *EndIfLenPtr = EndIfLen; StringRef EndIfStr(EndIfData, EndIfLen); + EndIfStr = EndIfStr.substr(EndIfStr.find_first_not_of("#endif \t")); // Give up if there's an escaped newline. size_t FindEscapedNewline = EndIfStr.find_last_not_of(' '); @@ -166,8 +166,13 @@ public: EndIfStr[FindEscapedNewline] == '\\') return false; - return (EndIf.isValid() && !EndIfStr.endswith("// " + HeaderGuard.str()) && - !EndIfStr.endswith("/* " + HeaderGuard.str() + " */")); + if (!Check->shouldSuggestEndifComment(FileName) && + !(EndIfStr.startswith("//") || + (EndIfStr.startswith("/*") && EndIfStr.endswith("*/")))) + return false; + + return (EndIfStr != "// " + HeaderGuard.str()) && + (EndIfStr != "/* " + HeaderGuard.str() + " */"); } /// \brief Look for header guards that don't match the preferred style. Emit @@ -207,11 +212,10 @@ public: std::vector<FixItHint> &FixIts) { size_t EndIfLen; if (wouldFixEndifComment(FileName, EndIf, HeaderGuard, &EndIfLen)) { - std::string Correct = "endif // " + HeaderGuard.str(); FixIts.push_back(FixItHint::CreateReplacement( CharSourceRange::getCharRange(EndIf, EndIf.getLocWithOffset(EndIfLen)), - Correct)); + Check->formatEndIf(HeaderGuard))); } } @@ -261,7 +265,7 @@ public: << FixItHint::CreateInsertion( SM.getLocForEndOfFile(FID), Check->shouldSuggestEndifComment(FileName) - ? "\n#endif // " + CPPVar + "\n" + ? "\n#" + Check->formatEndIf(CPPVar) + "\n" : "\n#endif\n"); } } @@ -294,5 +298,9 @@ bool HeaderGuardCheck::shouldSuggestToAddHeaderGuard(StringRef FileName) { return FileName.endswith(".h"); } +std::string HeaderGuardCheck::formatEndIf(StringRef HeaderGuard) { + return "endif // " + HeaderGuard.str(); +} + } // namespace tidy } // namespace clang |