diff --git a/clang-tools-extra/clang-tidy/.clang-tidy b/clang-tools-extra/clang-tidy/.clang-tidy index 82d0df8697178..26dfeb4a1f743 100644 --- a/clang-tools-extra/clang-tidy/.clang-tidy +++ b/clang-tools-extra/clang-tidy/.clang-tidy @@ -7,7 +7,7 @@ Checks: > -bugprone-branch-clone, -bugprone-easily-swappable-parameters, -bugprone-narrowing-conversions, - -bugprone-unchecked-optional-access, + bugprone-unchecked-optional-access, -bugprone-unused-return-value, misc-const-correctness, modernize-*, diff --git a/clang-tools-extra/clang-tidy/ClangTidy.cpp b/clang-tools-extra/clang-tidy/ClangTidy.cpp index 4af328cd5110a..e2f581562e97d 100644 --- a/clang-tools-extra/clang-tidy/ClangTidy.cpp +++ b/clang-tools-extra/clang-tidy/ClangTidy.cpp @@ -213,7 +213,8 @@ class ErrorReporter { } const StringRef Code = Buffer.get()->getBuffer(); auto Style = format::getStyle( - *Context.getOptionsForFile(File).FormatStyle, File, "none"); + Context.getOptionsForFile(File).FormatStyle.value_or("none"), File, + "none"); if (!Style) { llvm::errs() << llvm::toString(Style.takeError()) << "\n"; continue; diff --git a/clang-tools-extra/clang-tidy/ClangTidyProfiling.cpp b/clang-tools-extra/clang-tidy/ClangTidyProfiling.cpp index 6fee154be448c..7f8b2e9b66209 100644 --- a/clang-tools-extra/clang-tidy/ClangTidyProfiling.cpp +++ b/clang-tools-extra/clang-tidy/ClangTidyProfiling.cpp @@ -43,10 +43,11 @@ void ClangTidyProfiling::printUserFriendlyTable(llvm::raw_ostream &OS, } void ClangTidyProfiling::printAsJSON(llvm::raw_ostream &OS, - llvm::TimerGroup &TG) { + llvm::TimerGroup &TG, + const StorageParams &Storage) { OS << "{\n"; - OS << R"("file": ")" << Storage->SourceFilename << "\",\n"; - OS << R"("timestamp": ")" << Storage->Timestamp << "\",\n"; + OS << R"("file": ")" << Storage.SourceFilename << "\",\n"; + OS << R"("timestamp": ")" << Storage.Timestamp << "\",\n"; OS << "\"profile\": {\n"; TG.printJSONValues(OS, ""); OS << "\n}\n"; @@ -54,10 +55,9 @@ void ClangTidyProfiling::printAsJSON(llvm::raw_ostream &OS, OS.flush(); } -void ClangTidyProfiling::storeProfileData(llvm::TimerGroup &TG) { - assert(Storage && "We should have a filename."); - - llvm::SmallString<256> OutputDirectory(Storage->StoreFilename); +void ClangTidyProfiling::storeProfileData(llvm::TimerGroup &TG, + const StorageParams &Storage) { + llvm::SmallString<256> OutputDirectory(Storage.StoreFilename); llvm::sys::path::remove_filename(OutputDirectory); if (const std::error_code EC = llvm::sys::fs::create_directories(OutputDirectory)) { @@ -67,14 +67,14 @@ void ClangTidyProfiling::storeProfileData(llvm::TimerGroup &TG) { } std::error_code EC; - llvm::raw_fd_ostream OS(Storage->StoreFilename, EC, llvm::sys::fs::OF_None); + llvm::raw_fd_ostream OS(Storage.StoreFilename, EC, llvm::sys::fs::OF_None); if (EC) { - llvm::errs() << "Error opening output file '" << Storage->StoreFilename + llvm::errs() << "Error opening output file '" << Storage.StoreFilename << "': " << EC.message() << "\n"; return; } - printAsJSON(OS, TG); + printAsJSON(OS, TG, Storage); } ClangTidyProfiling::ClangTidyProfiling(std::optional Storage) @@ -85,7 +85,7 @@ ClangTidyProfiling::~ClangTidyProfiling() { if (!Storage) printUserFriendlyTable(llvm::errs(), TG); else - storeProfileData(TG); + storeProfileData(TG, *Storage); } } // namespace clang::tidy diff --git a/clang-tools-extra/clang-tidy/ClangTidyProfiling.h b/clang-tools-extra/clang-tidy/ClangTidyProfiling.h index 59c213b181ef7..40efcc10e2c47 100644 --- a/clang-tools-extra/clang-tidy/ClangTidyProfiling.h +++ b/clang-tools-extra/clang-tidy/ClangTidyProfiling.h @@ -37,8 +37,9 @@ class ClangTidyProfiling { std::optional Storage; void printUserFriendlyTable(llvm::raw_ostream &OS, llvm::TimerGroup &TG); - void printAsJSON(llvm::raw_ostream &OS, llvm::TimerGroup &TG); - void storeProfileData(llvm::TimerGroup &TG); + void printAsJSON(llvm::raw_ostream &OS, llvm::TimerGroup &TG, + const StorageParams &Storage); + void storeProfileData(llvm::TimerGroup &TG, const StorageParams &Storage); public: llvm::StringMap Records; diff --git a/clang-tools-extra/clang-tidy/abseil/TimeSubtractionCheck.cpp b/clang-tools-extra/clang-tidy/abseil/TimeSubtractionCheck.cpp index 4ae49d285930d..b23b6ad25a23c 100644 --- a/clang-tools-extra/clang-tidy/abseil/TimeSubtractionCheck.cpp +++ b/clang-tools-extra/clang-tidy/abseil/TimeSubtractionCheck.cpp @@ -96,6 +96,8 @@ void TimeSubtractionCheck::registerMatchers(MatchFinder *Finder) { const std::string TimeInverse = (llvm::Twine("ToUnix") + ScaleName).str(); std::optional Scale = getScaleForTimeInverse(TimeInverse); assert(Scale && "Unknown scale encountered"); + if (!Scale) + continue; auto TimeInverseMatcher = callExpr(callee( functionDecl(hasName((llvm::Twine("::absl::") + TimeInverse).str())) diff --git a/clang-tools-extra/clang-tidy/bugprone/EasilySwappableParametersCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/EasilySwappableParametersCheck.cpp index a07a68c8a3e65..bdc451be77c08 100644 --- a/clang-tools-extra/clang-tidy/bugprone/EasilySwappableParametersCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/EasilySwappableParametersCheck.cpp @@ -1690,8 +1690,9 @@ class PassedToSameFunction { TargetIdx.emplace(Idx); assert(TargetIdx && "Matched, but didn't find index?"); - TargetParams[PassedParamOfThisFn].insert( - {CalledFn->getCanonicalDecl(), *TargetIdx}); + if (TargetIdx) + TargetParams[PassedParamOfThisFn].insert( + {CalledFn->getCanonicalDecl(), *TargetIdx}); } } diff --git a/clang-tools-extra/clang-tidy/bugprone/RedundantBranchConditionCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/RedundantBranchConditionCheck.cpp index 528c254dbe17e..8eced3ee61c33 100644 --- a/clang-tools-extra/clang-tidy/bugprone/RedundantBranchConditionCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/RedundantBranchConditionCheck.cpp @@ -132,17 +132,18 @@ void RedundantBranchConditionCheck::check( // If the other side has side effects then keep it. if (OtherSide && OtherSide->HasSideEffects(*Result.Context)) { - const SourceLocation BeforeOtherSide = - OtherSide->getBeginLoc().getLocWithOffset(-1); - const SourceLocation AfterOtherSide = - Lexer::findNextToken(OtherSide->getEndLoc(), *Result.SourceManager, - getLangOpts()) - ->getLocation(); - Diag << FixItHint::CreateRemoval( - CharSourceRange::getTokenRange(IfBegin, BeforeOtherSide)) - << FixItHint::CreateInsertion(AfterOtherSide, ";") - << FixItHint::CreateRemoval( - CharSourceRange::getTokenRange(AfterOtherSide, IfEnd)); + auto NextToken = Lexer::findNextToken( + OtherSide->getEndLoc(), *Result.SourceManager, getLangOpts()); + if (NextToken) { + const SourceLocation BeforeOtherSide = + OtherSide->getBeginLoc().getLocWithOffset(-1); + const SourceLocation AfterOtherSide = NextToken->getLocation(); + Diag << FixItHint::CreateRemoval( + CharSourceRange::getTokenRange(IfBegin, BeforeOtherSide)) + << FixItHint::CreateInsertion(AfterOtherSide, ";") + << FixItHint::CreateRemoval( + CharSourceRange::getTokenRange(AfterOtherSide, IfEnd)); + } } else { Diag << FixItHint::CreateRemoval( CharSourceRange::getTokenRange(IfBegin, IfEnd)); @@ -166,12 +167,13 @@ void RedundantBranchConditionCheck::check( Diag << FixItHint::CreateRemoval(CharSourceRange::getTokenRange( CondOp->getLHS()->getBeginLoc(), BeforeRHS)); } else { - const SourceLocation AfterLHS = - Lexer::findNextToken(CondOp->getLHS()->getEndLoc(), - *Result.SourceManager, getLangOpts()) - ->getLocation(); - Diag << FixItHint::CreateRemoval(CharSourceRange::getTokenRange( - AfterLHS, CondOp->getRHS()->getEndLoc())); + auto NextToken = Lexer::findNextToken( + CondOp->getLHS()->getEndLoc(), *Result.SourceManager, getLangOpts()); + if (NextToken) { + const SourceLocation AfterLHS = NextToken->getLocation(); + Diag << FixItHint::CreateRemoval(CharSourceRange::getTokenRange( + AfterLHS, CondOp->getRHS()->getEndLoc())); + } } } } diff --git a/clang-tools-extra/clang-tidy/readability/FunctionSizeCheck.cpp b/clang-tools-extra/clang-tidy/readability/FunctionSizeCheck.cpp index 2f0949c231844..b80eed3f12971 100644 --- a/clang-tools-extra/clang-tidy/readability/FunctionSizeCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/FunctionSizeCheck.cpp @@ -225,10 +225,12 @@ void FunctionSizeCheck::check(const MatchFinder::MatchResult &Result) { << ActualNumberParameters << ParameterThreshold.value(); } - for (const auto &CSPos : FI.NestingThresholders) { - diag(CSPos, "nesting level %0 starts here (threshold %1)", - DiagnosticIDs::Note) - << NestingThreshold.value() + 1 << NestingThreshold.value(); + if (NestingThreshold) { + for (const auto &CSPos : FI.NestingThresholders) { + diag(CSPos, "nesting level %0 starts here (threshold %1)", + DiagnosticIDs::Note) + << NestingThreshold.value() + 1 << NestingThreshold.value(); + } } if (VariableThreshold && FI.Variables > VariableThreshold) { diff --git a/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp b/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp index d1583a62a8e5e..a4dda7e5e3285 100644 --- a/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp @@ -440,7 +440,7 @@ bool IdentifierNamingCheck::HungarianNotation::isOptionEnabled( if (Iter == StrMap.end()) return false; - return *llvm::yaml::parseBool(Iter->getValue()); + return llvm::yaml::parseBool(Iter->getValue()).value_or(false); } void IdentifierNamingCheck::HungarianNotation::loadFileConfig( @@ -834,26 +834,28 @@ void IdentifierNamingCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { const ArrayRef> Styles = MainFileStyle->getStyles(); for (size_t I = 0; I < SK_Count; ++I) { - if (!Styles[I]) + const auto &StyleOpt = Styles[I]; + if (!StyleOpt) continue; + const NamingStyle &Style = *StyleOpt; const size_t StyleSize = StyleNames[I].size(); StyleString.assign({StyleNames[I], "HungarianPrefix"}); - Options.store(Opts, StyleString, Styles[I]->HPType); + Options.store(Opts, StyleString, Style.HPType); memcpy(&StyleString[StyleSize], "IgnoredRegexp", 13); StyleString.truncate(StyleSize + 13); - Options.store(Opts, StyleString, Styles[I]->IgnoredRegexpStr); + Options.store(Opts, StyleString, Style.IgnoredRegexpStr); memcpy(&StyleString[StyleSize], "Prefix", 6); StyleString.truncate(StyleSize + 6); - Options.store(Opts, StyleString, Styles[I]->Prefix); + Options.store(Opts, StyleString, Style.Prefix); // Fast replacement of [Pre]fix -> [Suf]fix. memcpy(&StyleString[StyleSize], "Suf", 3); - Options.store(Opts, StyleString, Styles[I]->Suffix); - if (Styles[I]->Case) { + Options.store(Opts, StyleString, Style.Suffix); + if (Style.Case) { memcpy(&StyleString[StyleSize], "Case", 4); StyleString.pop_back_n(2); - Options.store(Opts, StyleString, *Styles[I]->Case); + Options.store(Opts, StyleString, *Style.Case); } } Options.store(Opts, "GetConfigPerFile", GetConfigPerFile); @@ -1344,10 +1346,14 @@ IdentifierNamingCheck::getFailureInfo( ArrayRef> NamingStyles, const IdentifierNamingCheck::HungarianNotationOption &HNOption, StyleKind SK, const SourceManager &SM, bool IgnoreFailedSplit) const { - if (SK == SK_Invalid || !NamingStyles[SK]) + if (SK == SK_Invalid) return std::nullopt; - const IdentifierNamingCheck::NamingStyle &Style = *NamingStyles[SK]; + const auto &StyleOpt = NamingStyles[SK]; + if (!StyleOpt) + return std::nullopt; + + const IdentifierNamingCheck::NamingStyle &Style = *StyleOpt; if (Style.IgnoredRegexp.isValid() && Style.IgnoredRegexp.match(Name)) return std::nullopt; diff --git a/clang-tools-extra/clang-tidy/utils/ExceptionAnalyzer.cpp b/clang-tools-extra/clang-tidy/utils/ExceptionAnalyzer.cpp index f766a1bca655c..428e3822c78c6 100644 --- a/clang-tools-extra/clang-tidy/utils/ExceptionAnalyzer.cpp +++ b/clang-tools-extra/clang-tidy/utils/ExceptionAnalyzer.cpp @@ -304,6 +304,9 @@ static bool isQualificationConvertiblePointer(QualType From, QualType To, "From pointer or array has no pointee or element!"); assert(ToPointeeOrElem && "To pointer or array has no pointee or element!"); + if (!FromPointeeOrElem || !ToPointeeOrElem) + return false; + From = *FromPointeeOrElem; To = *ToPointeeOrElem; } diff --git a/clang-tools-extra/clang-tidy/utils/FormatStringConverter.cpp b/clang-tools-extra/clang-tidy/utils/FormatStringConverter.cpp index d210b000dfd33..ab830d8d455d9 100644 --- a/clang-tools-extra/clang-tidy/utils/FormatStringConverter.cpp +++ b/clang-tools-extra/clang-tidy/utils/FormatStringConverter.cpp @@ -800,9 +800,11 @@ void FormatStringConverter::applyFixes(DiagnosticBuilder &Diag, } for (const auto &[ArgIndex, Replacement] : ArgFixes) { - const SourceLocation AfterOtherSide = - Lexer::findNextToken(Args[ArgIndex]->getEndLoc(), SM, LangOpts) - ->getLocation(); + const auto NextToken = + Lexer::findNextToken(Args[ArgIndex]->getEndLoc(), SM, LangOpts); + if (!NextToken) + continue; + const SourceLocation AfterOtherSide = NextToken->getLocation(); Diag << FixItHint::CreateInsertion(Args[ArgIndex]->getBeginLoc(), Replacement, true) diff --git a/clang-tools-extra/clang-tidy/utils/UseRangesCheck.cpp b/clang-tools-extra/clang-tidy/utils/UseRangesCheck.cpp index 09adbf1155e62..5cd88a72e83e1 100644 --- a/clang-tools-extra/clang-tidy/utils/UseRangesCheck.cpp +++ b/clang-tools-extra/clang-tidy/utils/UseRangesCheck.cpp @@ -247,6 +247,8 @@ void UseRangesCheck::check(const MatchFinder::MatchResult &Result) { Result.Context->getLangOpts())); } else { assert(ReverseDescriptor && "Couldn't find forward argument"); + if (!ReverseDescriptor) + return; ArgNode.push_back('R'); ArgExpr = Result.Nodes.getNodeAs(ArgNode); assert(ArgExpr && "Couldn't find forward or reverse argument");