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 @@ -163,7 +163,7 @@ static bool isZero(const Expr *E) {
case Stmt::IntegerLiteralClass:
return !cast<IntegerLiteral>(E)->getValue();
case Stmt::FloatingLiteralClass: {
llvm::APFloat Value = cast<FloatingLiteral>(E)->getValue();
const llvm::APFloat Value = cast<FloatingLiteral>(E)->getValue();
return Value.isZero() && !Value.isNegative();
}
default:
Expand Down Expand Up @@ -297,16 +297,16 @@ void UseDefaultMemberInitCheck::checkDefaultInit(
}) > 1)
return;

SourceLocation StartLoc = Field->getBeginLoc();
const SourceLocation StartLoc = Field->getBeginLoc();
if (StartLoc.isMacroID() && IgnoreMacros)
return;

SourceLocation FieldEnd =
const SourceLocation FieldEnd =
Lexer::getLocForEndOfToken(Field->getSourceRange().getEnd(), 0,
*Result.SourceManager, getLangOpts());
SourceLocation LParenEnd = Lexer::getLocForEndOfToken(
const SourceLocation LParenEnd = Lexer::getLocForEndOfToken(
Init->getLParenLoc(), 0, *Result.SourceManager, getLangOpts());
CharSourceRange InitRange =
const CharSourceRange InitRange =
CharSourceRange::getCharRange(LParenEnd, Init->getRParenLoc());

const Expr *InitExpression = Init->getInit();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ void UseDesignatedInitializersCheck::check(
if (IgnoreMacros && InitList->getBeginLoc().isMacroID())
return;
{
DiagnosticBuilder Diag =
const DiagnosticBuilder Diag =
diag(InitList->getLBraceLoc(),
"use designated initializer list to initialize %0");
Diag << InitList->getType() << InitList->getSourceRange();
Expand Down
10 changes: 5 additions & 5 deletions clang-tools-extra/clang-tidy/modernize/UseEqualsDefaultCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ static bool isCopyAssignmentAndCanBeDefaulted(ASTContext *Context,
/// Returns false if the body has any non-whitespace character.
static bool bodyEmpty(const ASTContext *Context, const CompoundStmt *Body) {
bool Invalid = false;
StringRef Text = Lexer::getSourceText(
const StringRef Text = Lexer::getSourceText(
CharSourceRange::getCharRange(Body->getLBracLoc().getLocWithOffset(1),
Body->getRBracLoc()),
Context->getSourceManager(), Context->getLangOpts(), &Invalid);
Expand Down Expand Up @@ -306,8 +306,8 @@ void UseEqualsDefaultCheck::check(const MatchFinder::MatchResult &Result) {
return;

// If there are comments inside the body, don't do the change.
bool ApplyFix = SpecialFunctionDecl->isCopyAssignmentOperator() ||
bodyEmpty(Result.Context, Body);
const bool ApplyFix = SpecialFunctionDecl->isCopyAssignmentOperator() ||
bodyEmpty(Result.Context, Body);

std::vector<FixItHint> RemoveInitializers;
unsigned MemberType = 0;
Expand Down Expand Up @@ -345,14 +345,14 @@ void UseEqualsDefaultCheck::check(const MatchFinder::MatchResult &Result) {
Diag << MemberType;

if (ApplyFix) {
SourceLocation UnifiedEnd = utils::lexer::getUnifiedEndLoc(
const SourceLocation UnifiedEnd = utils::lexer::getUnifiedEndLoc(
*Body, Result.Context->getSourceManager(),
Result.Context->getLangOpts());
// Skipping comments, check for a semicolon after Body->getSourceRange()
std::optional<Token> Token = utils::lexer::findNextTokenSkippingComments(
UnifiedEnd, Result.Context->getSourceManager(),
Result.Context->getLangOpts());
StringRef Replacement =
const StringRef Replacement =
Token && Token->is(tok::semi) ? "= default" : "= default;";
Diag << FixItHint::CreateReplacement(Body->getSourceRange(), Replacement)
<< RemoveInitializers;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ void UseEqualsDeleteCheck::registerMatchers(MatchFinder *Finder) {
void UseEqualsDeleteCheck::check(const MatchFinder::MatchResult &Result) {
if (const auto *Func =
Result.Nodes.getNodeAs<CXXMethodDecl>(SpecialFunction)) {
SourceLocation EndLoc = Lexer::getLocForEndOfToken(
const SourceLocation EndLoc = Lexer::getLocForEndOfToken(
Func->getEndLoc(), 0, *Result.SourceManager, getLangOpts());

if (IgnoreMacros && Func->getLocation().isMacroID())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ void UseIntegerSignComparisonCheck::check(
R3.setBegin(Lexer::getLocForEndOfToken(
SubExprRHS->getEndLoc(), 0, *Result.SourceManager, getLangOpts()));
}
DiagnosticBuilder Diag =
const DiagnosticBuilder Diag =
diag(BinaryOp->getBeginLoc(),
"comparison between 'signed' and 'unsigned' integers");
StringRef CmpNamespace;
Expand Down
4 changes: 2 additions & 2 deletions clang-tools-extra/clang-tidy/modernize/UseNodiscardCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,11 @@ void UseNodiscardCheck::registerMatchers(MatchFinder *Finder) {
void UseNodiscardCheck::check(const MatchFinder::MatchResult &Result) {
const auto *MatchedDecl = Result.Nodes.getNodeAs<CXXMethodDecl>("no_discard");
// Don't make replacements if the location is invalid or in a macro.
SourceLocation Loc = MatchedDecl->getLocation();
const SourceLocation Loc = MatchedDecl->getLocation();
if (Loc.isInvalid() || Loc.isMacroID())
return;

SourceLocation RetLoc = MatchedDecl->getInnerLocStart();
const SourceLocation RetLoc = MatchedDecl->getInnerLocStart();

ASTContext &Context = *Result.Context;

Expand Down
6 changes: 3 additions & 3 deletions clang-tools-extra/clang-tidy/modernize/UseNoexceptCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@ void UseNoexceptCheck::check(const MatchFinder::MatchResult &Result) {

assert(Range.isValid() && "Exception Source Range is invalid.");

CharSourceRange CRange = Lexer::makeFileCharRange(
const CharSourceRange CRange = Lexer::makeFileCharRange(
CharSourceRange::getTokenRange(Range), *Result.SourceManager,
Result.Context->getLangOpts());

bool IsNoThrow = FnTy->isNothrow();
StringRef ReplacementStr =
const bool IsNoThrow = FnTy->isNothrow();
const StringRef ReplacementStr =
IsNoThrow ? NoexceptMacro.empty() ? "noexcept" : NoexceptMacro
: NoexceptMacro.empty()
? (DtorOrOperatorDel || UseNoexceptFalse) ? "noexcept(false)" : ""
Expand Down
27 changes: 14 additions & 13 deletions clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,13 @@ static bool isReplaceableRange(SourceLocation StartLoc, SourceLocation EndLoc,
/// Returns true if and only if a replacement was made.
static void replaceWithNullptr(ClangTidyCheck &Check, SourceManager &SM,
SourceLocation StartLoc, SourceLocation EndLoc) {
CharSourceRange Range(SourceRange(StartLoc, EndLoc), true);
const CharSourceRange Range(SourceRange(StartLoc, EndLoc), true);
// Add a space if nullptr follows an alphanumeric character. This happens
// whenever there is an c-style explicit cast to nullptr not surrounded by
// parentheses and right beside a return statement.
SourceLocation PreviousLocation = StartLoc.getLocWithOffset(-1);
bool NeedsSpace = isAlphanumeric(*SM.getCharacterData(PreviousLocation));
const SourceLocation PreviousLocation = StartLoc.getLocWithOffset(-1);
const bool NeedsSpace =
isAlphanumeric(*SM.getCharacterData(PreviousLocation));
Check.diag(Range.getBegin(), "use nullptr") << FixItHint::CreateReplacement(
Range, NeedsSpace ? " nullptr" : "nullptr");
}
Expand Down Expand Up @@ -136,7 +137,7 @@ class MacroArgUsageVisitor : public RecursiveASTVisitor<MacroArgUsageVisitor> {
}

bool TraverseStmt(Stmt *S) {
bool VisitedPreviously = Visited;
const bool VisitedPreviously = Visited;

if (!RecursiveASTVisitor<MacroArgUsageVisitor>::TraverseStmt(S))
return false;
Expand Down Expand Up @@ -258,8 +259,8 @@ class CastSequenceVisitor : public RecursiveASTVisitor<CastSequenceVisitor> {
// If the location comes from a macro body expansion, check to see if its
// coming from one of the allowed 'NULL' macros.
if (SM.isMacroArgExpansion(StartLoc) && SM.isMacroArgExpansion(EndLoc)) {
SourceLocation FileLocStart = SM.getFileLoc(StartLoc),
FileLocEnd = SM.getFileLoc(EndLoc);
const SourceLocation FileLocStart = SM.getFileLoc(StartLoc),
FileLocEnd = SM.getFileLoc(EndLoc);
SourceLocation ImmediateMacroArgLoc, MacroLoc;
// Skip NULL macros used in macro.
if (!getMacroAndArgLocations(StartLoc, ImmediateMacroArgLoc, MacroLoc) ||
Expand All @@ -274,7 +275,7 @@ class CastSequenceVisitor : public RecursiveASTVisitor<CastSequenceVisitor> {
}

if (SM.isMacroBodyExpansion(StartLoc) && SM.isMacroBodyExpansion(EndLoc)) {
StringRef OutermostMacroName =
const StringRef OutermostMacroName =
getOutermostMacroName(StartLoc, SM, Context.getLangOpts());

// Check to see if the user wants to replace the macro being expanded.
Expand Down Expand Up @@ -302,7 +303,7 @@ class CastSequenceVisitor : public RecursiveASTVisitor<CastSequenceVisitor> {
/// Tests that all expansions of a macro arg, one of which expands to
/// result in \p CE, yield NullTo(Member)Pointer casts.
bool allArgUsesValid(const CastExpr *CE) {
SourceLocation CastLoc = CE->getBeginLoc();
const SourceLocation CastLoc = CE->getBeginLoc();

// Step 1: Get location of macro arg and location of the macro the arg was
// provided to.
Expand Down Expand Up @@ -348,17 +349,17 @@ class CastSequenceVisitor : public RecursiveASTVisitor<CastSequenceVisitor> {

// Find the location of the immediate macro expansion.
while (true) {
std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(ArgLoc);
const std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(ArgLoc);
const SrcMgr::SLocEntry *E = &SM.getSLocEntry(LocInfo.first);
const SrcMgr::ExpansionInfo &Expansion = E->getExpansion();

SourceLocation OldArgLoc = ArgLoc;
const SourceLocation OldArgLoc = ArgLoc;
ArgLoc = Expansion.getExpansionLocStart();
if (!Expansion.isMacroArgExpansion()) {
if (!MacroLoc.isFileID())
return false;

StringRef Name =
const StringRef Name =
Lexer::getImmediateMacroName(OldArgLoc, SM, Context.getLangOpts());
return llvm::is_contained(NullMacros, Name);
}
Expand All @@ -371,7 +372,7 @@ class CastSequenceVisitor : public RecursiveASTVisitor<CastSequenceVisitor> {

// If spelling location resides in the same FileID as macro expansion
// location, it means there is no inner macro.
FileID MacroFID = SM.getFileID(MacroLoc);
const FileID MacroFID = SM.getFileID(MacroLoc);
if (SM.isInFileID(ArgLoc, MacroFID)) {
// Don't transform this case. If the characters that caused the
// null-conversion come from within a macro, they can't be changed.
Expand Down Expand Up @@ -401,7 +402,7 @@ class CastSequenceVisitor : public RecursiveASTVisitor<CastSequenceVisitor> {
SourceLocation Loc = TestLoc, MacroLoc;

while (true) {
std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc);
const std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc);
const SrcMgr::SLocEntry *E = &SM.getSLocEntry(LocInfo.first);
const SrcMgr::ExpansionInfo &Expansion = E->getExpansion();

Expand Down
31 changes: 16 additions & 15 deletions clang-tools-extra/clang-tidy/modernize/UseOverrideCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ void UseOverrideCheck::registerMatchers(MatchFinder *Finder) {
static SmallVector<Token, 16>
parseTokens(CharSourceRange Range, const MatchFinder::MatchResult &Result) {
const SourceManager &Sources = *Result.SourceManager;
std::pair<FileID, unsigned> LocInfo =
const std::pair<FileID, unsigned> LocInfo =
Sources.getDecomposedLoc(Range.getBegin());
StringRef File = Sources.getBufferData(LocInfo.first);
const StringRef File = Sources.getBufferData(LocInfo.first);
const char *TokenBegin = File.data() + LocInfo.second;
Lexer RawLexer(Sources.getLocForStartOfFile(LocInfo.first),
Result.Context->getLangOpts(), File.begin(), TokenBegin,
Expand Down Expand Up @@ -103,12 +103,12 @@ void UseOverrideCheck::check(const MatchFinder::MatchResult &Result) {
Method->isOutOfLine())
return;

bool HasVirtual = Method->isVirtualAsWritten();
bool HasOverride = Method->getAttr<OverrideAttr>();
bool HasFinal = Method->getAttr<FinalAttr>();
const bool HasVirtual = Method->isVirtualAsWritten();
const bool HasOverride = Method->getAttr<OverrideAttr>();
const bool HasFinal = Method->getAttr<FinalAttr>();

bool OnlyVirtualSpecified = HasVirtual && !HasOverride && !HasFinal;
unsigned KeywordCount = HasVirtual + HasOverride + HasFinal;
const bool OnlyVirtualSpecified = HasVirtual && !HasOverride && !HasFinal;
const unsigned KeywordCount = HasVirtual + HasOverride + HasFinal;

if ((!OnlyVirtualSpecified && KeywordCount == 1) ||
(!HasVirtual && HasOverride && HasFinal && AllowOverrideAndFinal))
Expand All @@ -120,12 +120,12 @@ void UseOverrideCheck::check(const MatchFinder::MatchResult &Result) {
} else if (KeywordCount == 0) {
Message = "annotate this function with '%0' or (rarely) '%1'";
} else {
StringRef Redundant =
const StringRef Redundant =
HasVirtual ? (HasOverride && HasFinal && !AllowOverrideAndFinal
? "'virtual' and '%0' are"
: "'virtual' is")
: "'%0' is";
StringRef Correct = HasFinal ? "'%1'" : "'%0'";
const StringRef Correct = HasFinal ? "'%1'" : "'%0'";

Message = (llvm::Twine(Redundant) +
" redundant since the function is already declared " + Correct)
Expand All @@ -135,7 +135,7 @@ void UseOverrideCheck::check(const MatchFinder::MatchResult &Result) {
auto Diag = diag(Method->getLocation(), Message)
<< OverrideSpelling << FinalSpelling;

CharSourceRange FileRange = Lexer::makeFileCharRange(
const CharSourceRange FileRange = Lexer::makeFileCharRange(
CharSourceRange::getTokenRange(Method->getSourceRange()), Sources,
getLangOpts());

Expand All @@ -151,9 +151,9 @@ void UseOverrideCheck::check(const MatchFinder::MatchResult &Result) {
if (!HasFinal && !HasOverride) {
SourceLocation InsertLoc;
std::string ReplacementText = (OverrideSpelling + " ").str();
SourceLocation MethodLoc = Method->getLocation();
const SourceLocation MethodLoc = Method->getLocation();

for (Token T : Tokens) {
for (const Token T : Tokens) {
if (T.is(tok::kw___attribute) &&
!Sources.isBeforeInTranslationUnit(T.getLocation(), MethodLoc)) {
InsertLoc = T.getLocation();
Expand All @@ -164,7 +164,7 @@ void UseOverrideCheck::check(const MatchFinder::MatchResult &Result) {
if (Method->hasAttrs()) {
for (const clang::Attr *A : Method->getAttrs()) {
if (!A->isImplicit() && !A->isInherited()) {
SourceLocation Loc =
const SourceLocation Loc =
Sources.getExpansionLoc(A->getRange().getBegin());
if ((!InsertLoc.isValid() ||
Sources.isBeforeInTranslationUnit(Loc, InsertLoc)) &&
Expand Down Expand Up @@ -221,13 +221,14 @@ void UseOverrideCheck::check(const MatchFinder::MatchResult &Result) {
}

if (HasFinal && HasOverride && !AllowOverrideAndFinal) {
SourceLocation OverrideLoc = Method->getAttr<OverrideAttr>()->getLocation();
const SourceLocation OverrideLoc =
Method->getAttr<OverrideAttr>()->getLocation();
Diag << FixItHint::CreateRemoval(
CharSourceRange::getTokenRange(OverrideLoc, OverrideLoc));
}

if (HasVirtual) {
for (Token Tok : Tokens) {
for (const Token Tok : Tokens) {
if (Tok.is(tok::kw_virtual)) {
std::optional<Token> NextToken =
utils::lexer::findNextTokenIncludingComments(
Expand Down
5 changes: 3 additions & 2 deletions clang-tools-extra/clang-tidy/modernize/UseScopedLockCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ findLocksInCompoundStmt(const CompoundStmt *Block,

for (const Stmt *Stmt : Block->body()) {
if (const auto *DS = dyn_cast<DeclStmt>(Stmt)) {
llvm::SmallVector<const VarDecl *> LockGuards = getLockGuardsFromDecl(DS);
const llvm::SmallVector<const VarDecl *> LockGuards =
getLockGuardsFromDecl(DS);

if (!LockGuards.empty()) {
CurrentLockGuardGroup.append(LockGuards);
Expand Down Expand Up @@ -176,7 +177,7 @@ void UseScopedLockCheck::registerMatchers(MatchFinder *Finder) {

void UseScopedLockCheck::check(const MatchFinder::MatchResult &Result) {
if (const auto *DS = Result.Nodes.getNodeAs<DeclStmt>("lock-decl-single")) {
llvm::SmallVector<const VarDecl *> Decls = getLockGuardsFromDecl(DS);
const llvm::SmallVector<const VarDecl *> Decls = getLockGuardsFromDecl(DS);
diagOnMultipleLocks({Decls}, Result);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ UseStdNumbersCheck::UseStdNumbersCheck(const StringRef Name,

void UseStdNumbersCheck::registerMatchers(MatchFinder *const Finder) {
const auto Matches = MatchBuilder{DiffThreshold};
std::vector<Matcher<clang::Stmt>> ConstantMatchers = {
const std::vector<Matcher<clang::Stmt>> ConstantMatchers = {
Matches.matchLog2Euler(), Matches.matchLog10Euler(),
Matches.matchEulerTopLevel(), Matches.matchEgamma(),
Matches.matchInvSqrtPi(), Matches.matchInvPi(),
Expand Down
Loading