diff options
Diffstat (limited to 'clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp')
| -rw-r--r-- | clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp | 71 |
1 files changed, 36 insertions, 35 deletions
diff --git a/clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp b/clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp index 65fd09f..16a4d13 100644 --- a/clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp +++ b/clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp @@ -61,7 +61,7 @@ protected: // FIXME: Remove this once there's a better way to pass check names than // appending the check name to the message in ClangTidyContext::diag and // using getCustomDiagID. - std::string CheckNameInMessage = " [" + Error.DiagnosticName + "]"; + const std::string CheckNameInMessage = " [" + Error.DiagnosticName + "]"; Message.consume_back(CheckNameInMessage); auto TidyMessage = @@ -77,7 +77,7 @@ protected: if (SourceRange.isCharRange()) return SourceRange; assert(SourceRange.isTokenRange()); - SourceLocation End = Lexer::getLocForEndOfToken( + const SourceLocation End = Lexer::getLocForEndOfToken( SourceRange.getEnd(), 0, Loc.getManager(), LangOpts); return CharSourceRange::getCharRange(SourceRange.getBegin(), End); }; @@ -114,14 +114,14 @@ protected: Level == DiagnosticsEngine::Note ? &Error.Notes.back() : &Error.Message; for (const auto &FixIt : Hints) { - CharSourceRange Range = FixIt.RemoveRange; + const CharSourceRange Range = FixIt.RemoveRange; assert(Range.getBegin().isValid() && Range.getEnd().isValid() && "Invalid range in the fix-it hint."); assert(Range.getBegin().isFileID() && Range.getEnd().isFileID() && "Only file locations supported in fix-it hints."); - tooling::Replacement Replacement(Loc.getManager(), Range, - FixIt.CodeToInsert); + const tooling::Replacement Replacement(Loc.getManager(), Range, + FixIt.CodeToInsert); llvm::Error Err = DiagWithFix->Fix[Replacement.getFilePath()].add(Replacement); // FIXME: better error handling (at least, don't let other replacements be @@ -177,7 +177,7 @@ DiagnosticBuilder ClangTidyContext::diag( StringRef CheckName, SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level /* = DiagnosticIDs::Warning*/) { assert(Loc.isValid()); - unsigned ID = DiagEngine->getDiagnosticIDs()->getCustomDiagID( + const unsigned ID = DiagEngine->getDiagnosticIDs()->getCustomDiagID( Level, (Description + " [" + CheckName + "]").str()); CheckNamesByDiagnosticID.try_emplace(ID, CheckName); return DiagEngine->Report(Loc, ID); @@ -186,7 +186,7 @@ DiagnosticBuilder ClangTidyContext::diag( DiagnosticBuilder ClangTidyContext::diag( StringRef CheckName, StringRef Description, DiagnosticIDs::Level Level /* = DiagnosticIDs::Warning*/) { - unsigned ID = DiagEngine->getDiagnosticIDs()->getCustomDiagID( + const unsigned ID = DiagEngine->getDiagnosticIDs()->getCustomDiagID( Level, (Description + " [" + CheckName + "]").str()); CheckNamesByDiagnosticID.try_emplace(ID, CheckName); return DiagEngine->Report(ID); @@ -195,10 +195,11 @@ DiagnosticBuilder ClangTidyContext::diag( DiagnosticBuilder ClangTidyContext::diag(const tooling::Diagnostic &Error) { SourceManager &SM = DiagEngine->getSourceManager(); FileManager &FM = SM.getFileManager(); - FileEntryRef File = llvm::cantFail(FM.getFileRef(Error.Message.FilePath)); - FileID ID = SM.getOrCreateFileID(File, SrcMgr::C_User); - SourceLocation FileStartLoc = SM.getLocForStartOfFile(ID); - SourceLocation Loc = FileStartLoc.getLocWithOffset( + const FileEntryRef File = + llvm::cantFail(FM.getFileRef(Error.Message.FilePath)); + const FileID ID = SM.getOrCreateFileID(File, SrcMgr::C_User); + const SourceLocation FileStartLoc = SM.getLocForStartOfFile(ID); + const SourceLocation Loc = FileStartLoc.getLocWithOffset( static_cast<SourceLocation::IntTy>(Error.Message.FileOffset)); return diag(Error.DiagnosticName, Loc, Error.Message.Message, static_cast<DiagnosticIDs::Level>(Error.DiagLevel)); @@ -214,7 +215,7 @@ bool ClangTidyContext::shouldSuppressDiagnostic( DiagnosticsEngine::Level DiagLevel, const Diagnostic &Info, SmallVectorImpl<tooling::Diagnostic> &NoLintErrors, bool AllowIO, bool EnableNoLintBlocks) { - std::string CheckName = getCheckName(Info.getID()); + const std::string CheckName = getCheckName(Info.getID()); return NoLintHandler.shouldSuppress(DiagLevel, Info, CheckName, NoLintErrors, AllowIO, EnableNoLintBlocks); } @@ -226,7 +227,7 @@ void ClangTidyContext::setSourceManager(SourceManager *SourceMgr) { static bool parseFileExtensions(llvm::ArrayRef<std::string> AllFileExtensions, FileExtensionsSet &FileExtensions) { FileExtensions.clear(); - for (StringRef Suffix : AllFileExtensions) { + for (const StringRef Suffix : AllFileExtensions) { StringRef Extension = Suffix.trim(); if (!llvm::all_of(Extension, isAlphanumeric)) return false; @@ -294,11 +295,11 @@ bool ClangTidyContext::treatAsError(StringRef CheckName) const { } std::string ClangTidyContext::getCheckName(unsigned DiagnosticID) const { - std::string ClangWarningOption = std::string( + const std::string ClangWarningOption = std::string( DiagEngine->getDiagnosticIDs()->getWarningOptionForDiag(DiagnosticID)); if (!ClangWarningOption.empty()) return "clang-diagnostic-" + ClangWarningOption; - llvm::DenseMap<unsigned, std::string>::const_iterator I = + const llvm::DenseMap<unsigned, std::string>::const_iterator I = CheckNamesByDiagnosticID.find(DiagnosticID); if (I != CheckNamesByDiagnosticID.end()) return I->second; @@ -316,7 +317,7 @@ ClangTidyDiagnosticConsumer::ClangTidyDiagnosticConsumer( void ClangTidyDiagnosticConsumer::finalizeLastError() { if (!Errors.empty()) { - ClangTidyError &Error = Errors.back(); + const ClangTidyError &Error = Errors.back(); if (Error.DiagnosticName == "clang-tidy-config") { // Never ignore these. } else if (!Context.isCheckEnabled(Error.DiagnosticName) && @@ -436,8 +437,8 @@ void ClangTidyDiagnosticConsumer::HandleDiagnostic( Level = ClangTidyError::Remark; } - bool IsWarningAsError = DiagLevel == DiagnosticsEngine::Warning && - Context.treatAsError(CheckName); + const bool IsWarningAsError = DiagLevel == DiagnosticsEngine::Warning && + Context.treatAsError(CheckName); Errors.emplace_back(CheckName, Level, Context.getCurrentBuildDirectory(), IsWarningAsError); } @@ -477,11 +478,10 @@ bool ClangTidyDiagnosticConsumer::passesLineFilter(StringRef FileName, if (FileName.ends_with(Filter.Name)) { if (Filter.LineRanges.empty()) return true; - for (const FileFilter::LineRange &Range : Filter.LineRanges) { - if (Range.first <= LineNumber && LineNumber <= Range.second) - return true; - } - return false; + return llvm::any_of( + Filter.LineRanges, [&](const FileFilter::LineRange &Range) { + return Range.first <= LineNumber && LineNumber <= Range.second; + }); } } return false; @@ -491,8 +491,9 @@ void ClangTidyDiagnosticConsumer::forwardDiagnostic(const Diagnostic &Info) { // Acquire a diagnostic ID also in the external diagnostics engine. auto DiagLevelAndFormatString = Context.getDiagLevelAndFormatString(Info.getID(), Info.getLocation()); - unsigned ExternalID = ExternalDiagEngine->getDiagnosticIDs()->getCustomDiagID( - DiagLevelAndFormatString.first, DiagLevelAndFormatString.second); + const unsigned ExternalID = + ExternalDiagEngine->getDiagnosticIDs()->getCustomDiagID( + DiagLevelAndFormatString.first, DiagLevelAndFormatString.second); // Forward the details. auto Builder = ExternalDiagEngine->Report(Info.getLocation(), ExternalID); @@ -501,7 +502,7 @@ void ClangTidyDiagnosticConsumer::forwardDiagnostic(const Diagnostic &Info) { for (auto Range : Info.getRanges()) Builder << Range; for (unsigned Index = 0; Index < Info.getNumArgs(); ++Index) { - DiagnosticsEngine::ArgumentKind Kind = Info.getArgKind(Index); + const DiagnosticsEngine::ArgumentKind Kind = Info.getArgKind(Index); switch (Kind) { case clang::DiagnosticsEngine::ak_std_string: Builder << Info.getArgStdStr(Index); @@ -525,7 +526,8 @@ void ClangTidyDiagnosticConsumer::forwardDiagnostic(const Diagnostic &Info) { Builder << Qualifiers::fromOpaqueValue(Info.getRawArg(Index)); break; case clang::DiagnosticsEngine::ak_qualtype: - Builder << QualType::getFromOpaquePtr((void *)Info.getRawArg(Index)); + Builder << QualType::getFromOpaquePtr( + reinterpret_cast<void *>(Info.getRawArg(Index))); break; case clang::DiagnosticsEngine::ak_declarationname: Builder << DeclarationName::getFromOpaqueInteger(Info.getRawArg(Index)); @@ -574,7 +576,7 @@ void ClangTidyDiagnosticConsumer::checkFilters(SourceLocation Location, // FIXME: We start with a conservative approach here, but the actual type of // location needed depends on the check (in particular, where this check wants // to apply fixes). - FileID FID = Sources.getDecomposedExpansionLoc(Location).first; + const FileID FID = Sources.getDecomposedExpansionLoc(Location).first; OptionalFileEntryRef File = Sources.getFileEntryRefForID(FID); // -DMACRO definitions on the command line have locations in a virtual buffer @@ -585,13 +587,13 @@ void ClangTidyDiagnosticConsumer::checkFilters(SourceLocation Location, return; } - StringRef FileName(File->getName()); + const StringRef FileName(File->getName()); LastErrorRelatesToUserCode = LastErrorRelatesToUserCode || Sources.isInMainFile(Location) || (getHeaderFilter()->match(FileName) && !getExcludeHeaderFilter()->match(FileName)); - unsigned LineNumber = Sources.getExpansionLineNumber(Location); + const unsigned LineNumber = Sources.getExpansionLineNumber(Location); LastErrorPassesLineFilter = LastErrorPassesLineFilter || passesLineFilter(FileName, LineNumber); } @@ -707,8 +709,8 @@ void ClangTidyDiagnosticConsumer::removeIncompatibleErrors() { for (unsigned I = 0; I < ErrorFixes.size(); ++I) { for (const auto &FileAndReplace : *ErrorFixes[I].second) { for (const auto &Replace : FileAndReplace.second) { - unsigned Begin = Replace.getOffset(); - unsigned End = Begin + Replace.getLength(); + const unsigned Begin = Replace.getOffset(); + const unsigned End = Begin + Replace.getLength(); auto &Events = FileEvents[Replace.getFilePath()]; if (Begin == End) { Events.emplace_back(Begin, End, Event::ET_Insert, I, Sizes[I]); @@ -767,7 +769,7 @@ struct LessClangTidyError { }; struct EqualClangTidyError { bool operator()(const ClangTidyError &LHS, const ClangTidyError &RHS) const { - LessClangTidyError Less; + const LessClangTidyError Less; return !Less(LHS, RHS) && !Less(RHS, LHS); } }; @@ -803,7 +805,7 @@ void ClangTidyDiagnosticConsumer::removeDuplicatedDiagnosticsOfAliasCheckers() { auto IT = Errors.begin(); while (IT != Errors.end()) { ClangTidyError &Error = *IT; - std::pair<UniqueErrorSet::iterator, bool> Inserted = + const std::pair<UniqueErrorSet::iterator, bool> Inserted = UniqueErrors.insert(&Error); // Unique error, we keep it and move along. @@ -817,7 +819,6 @@ void ClangTidyDiagnosticConsumer::removeDuplicatedDiagnosticsOfAliasCheckers() { (*Inserted.first)->Message.Fix; if (CandidateFix != ExistingFix) { - // In case of a conflict, don't suggest any fix-it. ExistingError.Message.Fix.clear(); ExistingError.Notes.emplace_back( |
