Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ static std::optional<Token>
findConstToRemove(const ParmVarDecl &Param,
const MatchFinder::MatchResult &Result) {

CharSourceRange FileRange = Lexer::makeFileCharRange(
const CharSourceRange FileRange = Lexer::makeFileCharRange(
CharSourceRange::getTokenRange(getTypeRange(Param)),
*Result.SourceManager, Result.Context->getLangOpts());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ void AvoidReturnWithVoidValueCheck::check(
Result.Nodes.getNodeAs<CompoundStmt>("compound_parent");
if (!StrictMode && !SurroundingBlock)
return;
DiagnosticBuilder Diag = diag(VoidReturn->getBeginLoc(),
"return statement within a void function "
"should not have a specified return value");
const DiagnosticBuilder Diag = diag(
VoidReturn->getBeginLoc(), "return statement within a void function "
"should not have a specified return value");
const SourceLocation SemicolonPos = utils::lexer::findNextTerminator(
VoidReturn->getEndLoc(), *Result.SourceManager, getLangOpts());
if (SemicolonPos.isInvalid())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ struct AvoidUnconditionalPreprocessorIfPPCallbacks : public PPCallbacks {

bool isImmutable(SourceManager &SM, const LangOptions &LangOpts,
SourceRange ConditionRange) {
SourceLocation Loc = ConditionRange.getBegin();
const SourceLocation Loc = ConditionRange.getBegin();
if (Loc.isMacroID())
return false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ namespace clang::tidy::readability {
static tok::TokenKind getTokenKind(SourceLocation Loc, const SourceManager &SM,
const LangOptions &LangOpts) {
Token Tok;
SourceLocation Beginning = Lexer::GetBeginningOfToken(Loc, SM, LangOpts);
const SourceLocation Beginning =
Lexer::GetBeginningOfToken(Loc, SM, LangOpts);
const bool Invalid = Lexer::getRawToken(Beginning, Tok, SM, LangOpts);
assert(!Invalid && "Expected a valid token.");

Expand All @@ -38,7 +39,7 @@ forwardSkipWhitespaceAndComments(SourceLocation Loc, const SourceManager &SM,
while (isWhitespace(*SM.getCharacterData(Loc)))
Loc = Loc.getLocWithOffset(1);

tok::TokenKind TokKind = getTokenKind(Loc, SM, LangOpts);
const tok::TokenKind TokKind = getTokenKind(Loc, SM, LangOpts);
if (TokKind != tok::comment)
return Loc;

Expand Down Expand Up @@ -80,7 +81,8 @@ void BracesAroundStatementsCheck::check(
} else if (const auto *S = Result.Nodes.getNodeAs<DoStmt>("do")) {
checkStmt(Result, S->getBody(), S->getDoLoc(), S->getWhileLoc());
} else if (const auto *S = Result.Nodes.getNodeAs<WhileStmt>("while")) {
SourceLocation StartLoc = findRParenLoc(S, SM, Context->getLangOpts());
const SourceLocation StartLoc =
findRParenLoc(S, SM, Context->getLangOpts());
if (StartLoc.isInvalid())
return;
checkStmt(Result, S->getBody(), StartLoc);
Expand All @@ -89,12 +91,14 @@ void BracesAroundStatementsCheck::check(
if (S->isConsteval())
return;

SourceLocation StartLoc = findRParenLoc(S, SM, Context->getLangOpts());
const SourceLocation StartLoc =
findRParenLoc(S, SM, Context->getLangOpts());
if (StartLoc.isInvalid())
return;
if (ForceBracesStmts.erase(S))
ForceBracesStmts.insert(S->getThen());
bool BracedIf = checkStmt(Result, S->getThen(), StartLoc, S->getElseLoc());
const bool BracedIf =
checkStmt(Result, S->getThen(), StartLoc, S->getElseLoc());
const Stmt *Else = S->getElse();
if (Else && BracedIf)
ForceBracesStmts.insert(Else);
Expand Down Expand Up @@ -125,15 +129,15 @@ BracesAroundStatementsCheck::findRParenLoc(const IfOrWhileStmt *S,
return {};
}

SourceLocation PastCondEndLoc =
const SourceLocation PastCondEndLoc =
Lexer::getLocForEndOfToken(CondEndLoc, 0, SM, LangOpts);
if (PastCondEndLoc.isInvalid())
return {};
SourceLocation RParenLoc =
forwardSkipWhitespaceAndComments(PastCondEndLoc, SM, LangOpts);
if (RParenLoc.isInvalid())
return {};
tok::TokenKind TokKind = getTokenKind(RParenLoc, SM, LangOpts);
const tok::TokenKind TokKind = getTokenKind(RParenLoc, SM, LangOpts);
if (TokKind != tok::r_paren)
return {};
return RParenLoc;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ findConstToRemove(const FunctionDecl *Def,
// written in the source (for out-of-line declarations). A FunctionDecl's
// "location" is the start of its name, so, when the name is unqualified, we
// use `getLocation()`.
SourceLocation NameBeginLoc = Def->getQualifier()
? Def->getQualifierLoc().getBeginLoc()
: Def->getLocation();
const SourceLocation NameBeginLoc = Def->getQualifier()
? Def->getQualifierLoc().getBeginLoc()
: Def->getLocation();
// Since either of the locs can be in a macro, use `makeFileCharRange` to be
// sure that we have a consistent `CharSourceRange`, located entirely in the
// source file.
CharSourceRange FileRange = Lexer::makeFileCharRange(
const CharSourceRange FileRange = Lexer::makeFileCharRange(
CharSourceRange::getCharRange(Def->getBeginLoc(), NameBeginLoc),
*Result.SourceManager, Result.Context->getLangOpts());

Expand Down Expand Up @@ -118,12 +118,12 @@ void ConstReturnTypeCheck::check(const MatchFinder::MatchResult &Result) {
(Def->getBeginLoc().isMacroID() || Def->getEndLoc().isMacroID()))
return;

CheckResult CR = checkDef(Def, Result);
const CheckResult CR = checkDef(Def, Result);
{
// Clang only supports one in-flight diagnostic at a time. So, delimit the
// scope of `Diagnostic` to allow further diagnostics after the scope. We
// use `getInnerLocStart` to get the start of the return type.
DiagnosticBuilder Diagnostic =
const DiagnosticBuilder Diagnostic =
diag(Def->getInnerLocStart(),
"return type %0 is 'const'-qualified at the top level, which may "
"reduce code readability without improving const correctness")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ void ContainerContainsCheck::check(const MatchFinder::MatchResult &Result) {
Result.Nodes.getNodeAs<Expr>("negativeComparison");
assert((!PositiveComparison || !NegativeComparison) &&
"only one of PositiveComparison or NegativeComparison should be set");
bool Negated = NegativeComparison != nullptr;
const bool Negated = NegativeComparison != nullptr;
const auto *Comparison = Negated ? NegativeComparison : PositiveComparison;
const StringRef ContainsFunName =
Result.Nodes.getNodeAs<CXXMethodDecl>("contains_fun")->getName();
Expand All @@ -121,7 +121,7 @@ void ContainerContainsCheck::check(const MatchFinder::MatchResult &Result) {
<< ContainsFunName;

// Don't fix it if it's in a macro invocation. Leave fixing it to the user.
SourceLocation FuncCallLoc = Comparison->getEndLoc();
const SourceLocation FuncCallLoc = Comparison->getEndLoc();
if (!FuncCallLoc.isValid() || FuncCallLoc.isMacroID())
return;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ void ContainerDataPointerCheck::check(const MatchFinder::MatchResult &Result) {
else if (ACE)
CE = ACE;

SourceRange SrcRange = CE->getSourceRange();
const SourceRange SrcRange = CE->getSourceRange();

std::string ReplacementText{
Lexer::getSourceText(CharSourceRange::getTokenRange(SrcRange),
Expand All @@ -116,7 +116,7 @@ void ContainerDataPointerCheck::check(const MatchFinder::MatchResult &Result) {
else
ReplacementText += ".data()";

FixItHint Hint =
const FixItHint Hint =
FixItHint::CreateReplacement(UO->getSourceRange(), ReplacementText);
diag(UO->getBeginLoc(),
"'data' should be used for accessing the data pointer instead of taking "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,16 @@ static SourceRange getLocationOfConst(const TypeSourceInfo *TSI,
const auto FTL = TSI->getTypeLoc().IgnoreParens().getAs<FunctionTypeLoc>();
assert(FTL);

SourceRange Range{FTL.getRParenLoc().getLocWithOffset(1),
FTL.getLocalRangeEnd()};
const SourceRange Range{FTL.getRParenLoc().getLocWithOffset(1),
FTL.getLocalRangeEnd()};
// Inside Range, there might be other keywords and trailing return types.
// Find the exact position of "const".
StringRef Text = getStringFromRange(SourceMgr, LangOpts, Range);
size_t Offset = Text.find("const");
const StringRef Text = getStringFromRange(SourceMgr, LangOpts, Range);
const size_t Offset = Text.find("const");
if (Offset == StringRef::npos)
return {};

SourceLocation Start = Range.getBegin().getLocWithOffset(Offset);
const SourceLocation Start = Range.getBegin().getLocWithOffset(Offset);
return {Start, Start.getLocWithOffset(strlen("const") - 1)};
}

Expand All @@ -138,7 +138,7 @@ void ConvertMemberFunctionsToStaticCheck::check(

// TODO: For out-of-line declarations, don't modify the source if the header
// is excluded by the -header-filter option.
DiagnosticBuilder Diag =
const DiagnosticBuilder Diag =
diag(Definition->getLocation(), "method %0 can be made static")
<< Definition;

Expand All @@ -153,15 +153,15 @@ void ConvertMemberFunctionsToStaticCheck::check(
if (Definition->isConst()) {
// Make sure that we either remove 'const' on both declaration and
// definition or emit no fix-it at all.
SourceRange DefConst = getLocationOfConst(Definition->getTypeSourceInfo(),
*Result.SourceManager,
Result.Context->getLangOpts());
const SourceRange DefConst = getLocationOfConst(
Definition->getTypeSourceInfo(), *Result.SourceManager,
Result.Context->getLangOpts());

if (DefConst.isInvalid())
return;

if (Declaration != Definition) {
SourceRange DeclConst = getLocationOfConst(
const SourceRange DeclConst = getLocationOfConst(
Declaration->getTypeSourceInfo(), *Result.SourceManager,
Result.Context->getLangOpts());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ void DuplicateIncludeCallbacks::InclusionDirective(
if (llvm::is_contained(Files.back(), FileName)) {
// We want to delete the entire line, so make sure that [Start,End] covers
// everything.
SourceLocation Start =
const SourceLocation Start =
advanceBeyondCurrentLine(SM, HashLoc, -1).getLocWithOffset(-1);
SourceLocation End =
const SourceLocation End =
advanceBeyondCurrentLine(SM, FilenameRange.getEnd(), 1);
Check.diag(HashLoc, "duplicate include")
<< FixItHint::CreateRemoval(SourceRange{Start, End});
Expand Down
26 changes: 13 additions & 13 deletions clang-tools-extra/clang-tidy/readability/ElseAfterReturnCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,21 +124,21 @@ static void removeElseAndBrackets(DiagnosticBuilder &Diag, ASTContext &Context,

if (const auto *CS = dyn_cast<CompoundStmt>(Else)) {
Diag << tooling::fixit::createRemoval(ElseLoc);
SourceLocation LBrace = CS->getLBracLoc();
SourceLocation RBrace = CS->getRBracLoc();
SourceLocation RangeStart =
const SourceLocation LBrace = CS->getLBracLoc();
const SourceLocation RBrace = CS->getRBracLoc();
const SourceLocation RangeStart =
Remap(LBrace).getLocWithOffset(TokLen(LBrace) + 1);
SourceLocation RangeEnd = Remap(RBrace).getLocWithOffset(-1);
const SourceLocation RangeEnd = Remap(RBrace).getLocWithOffset(-1);

llvm::StringRef Repl = Lexer::getSourceText(
const llvm::StringRef Repl = Lexer::getSourceText(
CharSourceRange::getTokenRange(RangeStart, RangeEnd),
Context.getSourceManager(), Context.getLangOpts());
Diag << tooling::fixit::createReplacement(CS->getSourceRange(), Repl);
} else {
SourceLocation ElseExpandedLoc = Remap(ElseLoc);
SourceLocation EndLoc = Remap(Else->getEndLoc());
const SourceLocation ElseExpandedLoc = Remap(ElseLoc);
const SourceLocation EndLoc = Remap(Else->getEndLoc());

llvm::StringRef Repl = Lexer::getSourceText(
const llvm::StringRef Repl = Lexer::getSourceText(
CharSourceRange::getTokenRange(
ElseExpandedLoc.getLocWithOffset(TokLen(ElseLoc) + 1), EndLoc),
Context.getSourceManager(), Context.getLangOpts());
Expand Down Expand Up @@ -186,8 +186,8 @@ static bool hasPreprocessorBranchEndBetweenLocations(
const ElseAfterReturnCheck::ConditionalBranchMap &ConditionalBranchMap,
const SourceManager &SM, SourceLocation StartLoc, SourceLocation EndLoc) {

SourceLocation ExpandedStartLoc = SM.getExpansionLoc(StartLoc);
SourceLocation ExpandedEndLoc = SM.getExpansionLoc(EndLoc);
const SourceLocation ExpandedStartLoc = SM.getExpansionLoc(StartLoc);
const SourceLocation ExpandedEndLoc = SM.getExpansionLoc(EndLoc);
if (!SM.isWrittenInSameFile(ExpandedStartLoc, ExpandedEndLoc))
return false;

Expand Down Expand Up @@ -239,14 +239,14 @@ void ElseAfterReturnCheck::check(const MatchFinder::MatchResult &Result) {
const auto *Else = Result.Nodes.getNodeAs<Stmt>("else");
const auto *OuterScope = Result.Nodes.getNodeAs<CompoundStmt>("cs");
const auto *Interrupt = Result.Nodes.getNodeAs<Stmt>(InterruptingStr);
SourceLocation ElseLoc = If->getElseLoc();
const SourceLocation ElseLoc = If->getElseLoc();

if (hasPreprocessorBranchEndBetweenLocations(
PPConditionals, *Result.SourceManager, Interrupt->getBeginLoc(),
ElseLoc))
return;

bool IsLastInScope = OuterScope->body_back() == If;
const bool IsLastInScope = OuterScope->body_back() == If;
const StringRef ControlFlowInterrupter = getControlFlowString(*Interrupt);

if (!IsLastInScope && containsDeclInScope(Else)) {
Expand Down Expand Up @@ -276,7 +276,7 @@ void ElseAfterReturnCheck::check(const MatchFinder::MatchResult &Result) {
}
const DeclStmt *VDeclStmt = If->getConditionVariableDeclStmt();
const VarDecl *VDecl = If->getConditionVariable();
std::string Repl =
const std::string Repl =
(tooling::fixit::getText(*VDeclStmt, *Result.Context) +
llvm::StringRef(";\n") +
tooling::fixit::getText(If->getIfLoc(), *Result.Context))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ static void cleanInitialValue(DiagnosticBuilder &Diag,
namespace {

AST_MATCHER(EnumDecl, isMacro) {
SourceLocation Loc = Node.getBeginLoc();
const SourceLocation Loc = Node.getBeginLoc();
return Loc.isMacroID();
}

Expand Down Expand Up @@ -165,7 +165,7 @@ void EnumInitialValueCheck::registerMatchers(MatchFinder *Finder) {

void EnumInitialValueCheck::check(const MatchFinder::MatchResult &Result) {
if (const auto *Enum = Result.Nodes.getNodeAs<EnumDecl>("inconsistent")) {
DiagnosticBuilder Diag =
const DiagnosticBuilder Diag =
diag(
Enum->getBeginLoc(),
"initial values in enum '%0' are not consistent, consider explicit "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,14 +229,14 @@ class FunctionASTVisitor final

bool traverseStmtWithIncreasedNestingLevel(Stmt *Node) {
++CurrentNestingLevel;
bool ShouldContinue = Base::TraverseStmt(Node);
const bool ShouldContinue = Base::TraverseStmt(Node);
--CurrentNestingLevel;
return ShouldContinue;
}

bool traverseDeclWithIncreasedNestingLevel(Decl *Node) {
++CurrentNestingLevel;
bool ShouldContinue = Base::TraverseDecl(Node);
const bool ShouldContinue = Base::TraverseDecl(Node);
--CurrentNestingLevel;
return ShouldContinue;
}
Expand Down Expand Up @@ -336,7 +336,7 @@ class FunctionASTVisitor final

// Record the operator that we are currently processing and traverse it.
CurrentBinaryOperator = Op->getOpcode();
bool ShouldContinue = Base::TraverseBinaryOperator(Op);
const bool ShouldContinue = Base::TraverseBinaryOperator(Op);

// And restore the previous binary operator, which might be nonexistent.
CurrentBinaryOperator = BinOpCopy;
Expand All @@ -354,7 +354,7 @@ class FunctionASTVisitor final

// Else, do add [uninitialized] frame to the stack, and traverse call.
BinaryOperatorsStack.emplace();
bool ShouldContinue = Base::TraverseCallExpr(Node);
const bool ShouldContinue = Base::TraverseCallExpr(Node);
// And remove the top frame.
BinaryOperatorsStack.pop();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,14 @@ void FunctionSizeCheck::check(const MatchFinder::MatchResult &Result) {

// Count the lines including whitespace and comments. Really simple.
if (const Stmt *Body = Func->getBody()) {
SourceManager *SM = Result.SourceManager;
const SourceManager *SM = Result.SourceManager;
if (SM->isWrittenInSameFile(Body->getBeginLoc(), Body->getEndLoc())) {
FI.Lines = SM->getSpellingLineNumber(Body->getEndLoc()) -
SM->getSpellingLineNumber(Body->getBeginLoc());
}
}

unsigned ActualNumberParameters = Func->getNumParams();
const unsigned ActualNumberParameters = Func->getNumParams();

if ((LineThreshold && FI.Lines > LineThreshold) ||
(StatementThreshold && FI.Statements > StatementThreshold) ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ void IdentifierLengthCheck::check(const MatchFinder::MatchResult &Result) {
if (!StandaloneVar->getIdentifier())
return;

StringRef VarName = StandaloneVar->getName();
const StringRef VarName = StandaloneVar->getName();

if (VarName.size() >= MinimumVariableNameLength ||
IgnoredVariableNames.match(VarName))
Expand All @@ -106,7 +106,7 @@ void IdentifierLengthCheck::check(const MatchFinder::MatchResult &Result) {
if (!ExceptionVarName->getIdentifier())
return;

StringRef VarName = ExceptionVarName->getName();
const StringRef VarName = ExceptionVarName->getName();
if (VarName.size() >= MinimumExceptionNameLength ||
IgnoredExceptionVariableNames.match(VarName))
return;
Expand All @@ -120,7 +120,7 @@ void IdentifierLengthCheck::check(const MatchFinder::MatchResult &Result) {
if (!LoopVar->getIdentifier())
return;

StringRef VarName = LoopVar->getName();
const StringRef VarName = LoopVar->getName();

if (VarName.size() >= MinimumLoopCounterNameLength ||
IgnoredLoopCounterNames.match(VarName))
Expand All @@ -135,7 +135,7 @@ void IdentifierLengthCheck::check(const MatchFinder::MatchResult &Result) {
if (!ParamVar->getIdentifier())
return;

StringRef VarName = ParamVar->getName();
const StringRef VarName = ParamVar->getName();

if (VarName.size() >= MinimumParameterNameLength ||
IgnoredParameterNames.match(VarName))
Expand Down
Loading