diff options
Diffstat (limited to 'clang-tools-extra/clang-tidy/utils/HeaderGuard.cpp')
| -rw-r--r-- | clang-tools-extra/clang-tidy/utils/HeaderGuard.cpp | 41 |
1 files changed, 21 insertions, 20 deletions
diff --git a/clang-tools-extra/clang-tidy/utils/HeaderGuard.cpp b/clang-tools-extra/clang-tidy/utils/HeaderGuard.cpp index e1d1387..d36b187 100644 --- a/clang-tools-extra/clang-tidy/utils/HeaderGuard.cpp +++ b/clang-tools-extra/clang-tidy/utils/HeaderGuard.cpp @@ -32,11 +32,11 @@ public: FileID PrevFID) override { // Record all files we enter. We'll need them to diagnose headers without // guards. - SourceManager &SM = PP->getSourceManager(); + const SourceManager &SM = PP->getSourceManager(); if (Reason == EnterFile && FileType == SrcMgr::C_User) { if (OptionalFileEntryRef FE = SM.getFileEntryRefForID(SM.getFileID(Loc))) { - std::string FileName = cleanPath(FE->getName()); + const std::string FileName = cleanPath(FE->getName()); Files[FileName] = *FE; } } @@ -66,7 +66,7 @@ public: void EndOfMainFile() override { // Now that we have all this information from the preprocessor, use it! - SourceManager &SM = PP->getSourceManager(); + const SourceManager &SM = PP->getSourceManager(); for (const auto &MacroEntry : Macros) { const MacroInfo *MI = MacroEntry.second; @@ -79,7 +79,7 @@ public: OptionalFileEntryRef FE = SM.getFileEntryRefForID(SM.getFileID(MI->getDefinitionLoc())); - std::string FileName = cleanPath(FE->getName()); + const std::string FileName = cleanPath(FE->getName()); Files.erase(FileName); // See if we should check and fix this header guard. @@ -88,16 +88,16 @@ public: // Look up Locations for this guard. const auto &Locs = Ifndefs[MacroEntry.first.getIdentifierInfo()]; - SourceLocation Ifndef = Locs.second; - SourceLocation Define = MacroEntry.first.getLocation(); - SourceLocation EndIf = EndIfs[Locs.first]; + const SourceLocation Ifndef = Locs.second; + const SourceLocation Define = MacroEntry.first.getLocation(); + const SourceLocation EndIf = EndIfs[Locs.first]; // If the macro Name is not equal to what we can compute, correct it in // the #ifndef and #define. - StringRef CurHeaderGuard = + const StringRef CurHeaderGuard = MacroEntry.first.getIdentifierInfo()->getName(); std::vector<FixItHint> FixIts; - std::string NewGuard = checkHeaderGuardDefinition( + const std::string NewGuard = checkHeaderGuardDefinition( Ifndef, Define, EndIf, FileName, CurHeaderGuard, FixIts); // Now look at the #endif. We want a comment with the header guard. Fix it @@ -129,7 +129,7 @@ public: if (!EndIf.isValid()) return false; const char *EndIfData = PP->getSourceManager().getCharacterData(EndIf); - size_t EndIfLen = std::strcspn(EndIfData, "\r\n"); + const size_t EndIfLen = std::strcspn(EndIfData, "\r\n"); if (EndIfLenPtr) *EndIfLenPtr = EndIfLen; @@ -137,12 +137,12 @@ public: 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(' '); + const size_t FindEscapedNewline = EndIfStr.find_last_not_of(' '); if (FindEscapedNewline != StringRef::npos && EndIfStr[FindEscapedNewline] == '\\') return false; - bool IsLineComment = + const bool IsLineComment = EndIfStr.consume_front("//") || (EndIfStr.consume_front("/*") && EndIfStr.consume_back("*/")); if (!IsLineComment) @@ -162,7 +162,7 @@ public: std::vector<FixItHint> &FixIts) { std::string CPPVar = Check->getHeaderGuard(FileName, CurHeaderGuard); CPPVar = Check->sanitizeHeaderGuard(CPPVar); - std::string CPPVarUnder = CPPVar + '_'; + const std::string CPPVarUnder = CPPVar + '_'; // Allow a trailing underscore if and only if we don't have to change the // endif comment too. @@ -203,19 +203,20 @@ public: // fix-its to add the guard. // TODO: Insert the guard after top comments. for (const auto &FE : Files) { - StringRef FileName = FE.getKey(); + const StringRef FileName = FE.getKey(); if (!Check->shouldSuggestToAddHeaderGuard(FileName)) continue; - SourceManager &SM = PP->getSourceManager(); - FileID FID = SM.translateFile(FE.getValue()); - SourceLocation StartLoc = SM.getLocForStartOfFile(FID); + const SourceManager &SM = PP->getSourceManager(); + const FileID FID = SM.translateFile(FE.getValue()); + const SourceLocation StartLoc = SM.getLocForStartOfFile(FID); if (StartLoc.isInvalid()) continue; std::string CPPVar = Check->getHeaderGuard(FileName); CPPVar = Check->sanitizeHeaderGuard(CPPVar); - std::string CPPVarUnder = CPPVar + '_'; // Allow a trailing underscore. + const std::string CPPVarUnder = + CPPVar + '_'; // Allow a trailing underscore. // If there's a macro with a name that follows the header guard convention // but was not recognized by the preprocessor as a header guard there must // be code outside of the guarded area. Emit a plain warning without @@ -223,8 +224,8 @@ public: // FIXME: Can we move it into the right spot? bool SeenMacro = false; for (const auto &MacroEntry : Macros) { - StringRef Name = MacroEntry.first.getIdentifierInfo()->getName(); - SourceLocation DefineLoc = MacroEntry.first.getLocation(); + const StringRef Name = MacroEntry.first.getIdentifierInfo()->getName(); + const SourceLocation DefineLoc = MacroEntry.first.getLocation(); if ((Name == CPPVar || Name == CPPVarUnder) && SM.isWrittenInSameFile(StartLoc, DefineLoc)) { Check->diag(DefineLoc, "code/includes outside of area guarded by " |
