Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion clang-tools-extra/clang-tidy/.clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -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-*,
Expand Down
3 changes: 2 additions & 1 deletion clang-tools-extra/clang-tidy/ClangTidy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
22 changes: 11 additions & 11 deletions clang-tools-extra/clang-tidy/ClangTidyProfiling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,21 @@ 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";
OS << "}\n";
OS.flush();
}

void ClangTidyProfiling::storeProfileData(llvm::TimerGroup &TG) {
assert(Storage && "We should have a filename.");
Copy link
Member Author

@zeyi2 zeyi2 Nov 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will fix the removal of this assert soon.


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)) {
Expand All @@ -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<StorageParams> Storage)
Expand All @@ -85,7 +85,7 @@ ClangTidyProfiling::~ClangTidyProfiling() {
if (!Storage)
printUserFriendlyTable(llvm::errs(), TG);
else
storeProfileData(TG);
storeProfileData(TG, *Storage);
}

} // namespace clang::tidy
5 changes: 3 additions & 2 deletions clang-tools-extra/clang-tidy/ClangTidyProfiling.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ class ClangTidyProfiling {
std::optional<StorageParams> 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<llvm::TimeRecord> Records;
Expand Down
2 changes: 2 additions & 0 deletions clang-tools-extra/clang-tidy/abseil/TimeSubtractionCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ void TimeSubtractionCheck::registerMatchers(MatchFinder *Finder) {
const std::string TimeInverse = (llvm::Twine("ToUnix") + ScaleName).str();
std::optional<DurationScale> Scale = getScaleForTimeInverse(TimeInverse);
assert(Scale && "Unknown scale encountered");
if (!Scale)
continue;

auto TimeInverseMatcher = callExpr(callee(
functionDecl(hasName((llvm::Twine("::absl::") + TimeInverse).str()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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});
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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()));
}
}
}
}
Expand Down
10 changes: 6 additions & 4 deletions clang-tools-extra/clang-tidy/readability/FunctionSizeCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
26 changes: 16 additions & 10 deletions clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -834,26 +834,28 @@ void IdentifierNamingCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
const ArrayRef<std::optional<NamingStyle>> 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);
Expand Down Expand Up @@ -1344,10 +1346,14 @@ IdentifierNamingCheck::getFailureInfo(
ArrayRef<std::optional<IdentifierNamingCheck::NamingStyle>> 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;

Expand Down
3 changes: 3 additions & 0 deletions clang-tools-extra/clang-tidy/utils/ExceptionAnalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
8 changes: 5 additions & 3 deletions clang-tools-extra/clang-tidy/utils/FormatStringConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions clang-tools-extra/clang-tidy/utils/UseRangesCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Expr>(ArgNode);
assert(ArgExpr && "Couldn't find forward or reverse argument");
Expand Down