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 @@ -129,7 +129,7 @@ void StringIntegerAssignmentCheck::check(
const auto *Argument = Result.Nodes.getNodeAs<Expr>("expr");
const auto CharType =
Result.Nodes.getNodeAs<QualType>("type")->getCanonicalType();
SourceLocation Loc = Argument->getBeginLoc();
const SourceLocation Loc = Argument->getBeginLoc();

// Try to detect a few common expressions to reduce false positives.
if (CharExpressionDetector(CharType, *Result.Context)
Expand All @@ -145,7 +145,7 @@ void StringIntegerAssignmentCheck::check(
if (Loc.isMacroID())
return;

bool IsWideCharType = CharType->isWideCharType();
const bool IsWideCharType = CharType->isWideCharType();
if (!CharType->isCharType() && !IsWideCharType)
return;
bool IsOneDigit = false;
Expand All @@ -155,7 +155,7 @@ void StringIntegerAssignmentCheck::check(
IsLiteral = true;
}

SourceLocation EndLoc = Lexer::getLocForEndOfToken(
const SourceLocation EndLoc = Lexer::getLocForEndOfToken(
Argument->getEndLoc(), 0, *Result.SourceManager, getLangOpts());
if (IsOneDigit) {
Diag << FixItHint::CreateInsertion(Loc, IsWideCharType ? "L'" : "'")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ static int enumLength(const EnumDecl *EnumDec) {

static bool hasDisjointValueRange(const EnumDecl *Enum1,
const EnumDecl *Enum2) {
ValueRange Range1(Enum1), Range2(Enum2);
const ValueRange Range1(Enum1), Range2(Enum2);
return llvm::APSInt::compareValues(Range1.MaxVal, Range2.MinVal) < 0 ||
llvm::APSInt::compareValues(Range2.MaxVal, Range1.MinVal) < 0;
}
Expand Down Expand Up @@ -94,9 +94,9 @@ static int countNonPowOfTwoLiteralNum(const EnumDecl *EnumDec) {
/// last enumerator is the sum of the lesser values (and initialized by a
/// literal) or when it could contain consecutive values.
static bool isPossiblyBitMask(const EnumDecl *EnumDec) {
ValueRange VR(EnumDec);
int EnumLen = enumLength(EnumDec);
int NonPowOfTwoCounter = countNonPowOfTwoLiteralNum(EnumDec);
const ValueRange VR(EnumDec);
const int EnumLen = enumLength(EnumDec);
const int NonPowOfTwoCounter = countNonPowOfTwoLiteralNum(EnumDec);
return NonPowOfTwoCounter >= 1 && NonPowOfTwoCounter <= 2 &&
NonPowOfTwoCounter < EnumLen / 2 &&
(VR.MaxVal - VR.MinVal != EnumLen - 1) &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ void SuspiciousIncludePPCallbacks::InclusionDirective(
if (!Check.IgnoredRegexString.empty() && Check.IgnoredRegex.match(FileName))
return;

SourceLocation DiagLoc = FilenameRange.getBegin().getLocWithOffset(1);
const SourceLocation DiagLoc = FilenameRange.getBegin().getLocWithOffset(1);

const std::optional<StringRef> IFE =
utils::getFileExtension(FileName, Check.ImplementationFileExtensions);
Expand All @@ -81,7 +81,7 @@ void SuspiciousIncludePPCallbacks::InclusionDirective(
llvm::sys::path::replace_extension(GuessedFileName,
(!HFE.empty() ? "." : "") + HFE);

OptionalFileEntryRef File =
const OptionalFileEntryRef File =
PP->LookupFile(DiagLoc, GuessedFileName, IsAngled, nullptr, nullptr,
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr);
if (File) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ void SuspiciousMemoryComparisonCheck::check(

for (unsigned int ArgIndex = 0; ArgIndex < 2; ++ArgIndex) {
const Expr *ArgExpr = CE->getArg(ArgIndex);
QualType ArgType = ArgExpr->IgnoreImplicit()->getType();
const QualType ArgType = ArgExpr->IgnoreImplicit()->getType();
const Type *PointeeType = ArgType->getPointeeOrArrayElementType();
assert(PointeeType != nullptr && "PointeeType should always be available.");
QualType PointeeQualifiedType(PointeeType, 0);
const QualType PointeeQualifiedType(PointeeType, 0);

if (PointeeType->isRecordType()) {
if (const RecordDecl *RD =
Expand All @@ -65,7 +65,7 @@ void SuspiciousMemoryComparisonCheck::check(
}

if (!PointeeType->isIncompleteType()) {
uint64_t PointeeSize = Ctx.getTypeSize(PointeeType);
const uint64_t PointeeSize = Ctx.getTypeSize(PointeeType);
if (ComparedBits && *ComparedBits >= PointeeSize &&
!Ctx.hasUniqueObjectRepresentations(PointeeQualifiedType)) {
diag(CE->getBeginLoc(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ void SuspiciousMemsetUsageCheck::check(const MatchFinder::MatchResult &Result) {
// Case 1: fill_char of memset() is a character '0'. Probably an
// integer zero was intended.

SourceRange CharRange = CharZeroFill->getSourceRange();
const SourceRange CharRange = CharZeroFill->getSourceRange();
auto Diag =
diag(CharZeroFill->getBeginLoc(), "memset fill value is char '0', "
"potentially mistaken for int 0");
Expand All @@ -82,7 +82,7 @@ void SuspiciousMemsetUsageCheck::check(const MatchFinder::MatchResult &Result) {
if (!NumFill->EvaluateAsInt(EVResult, *Result.Context))
return;

llvm::APSInt NumValue = EVResult.Val.getInt();
const llvm::APSInt NumValue = EVResult.Val.getInt();
if (NumValue >= 0 && NumValue <= UCharMax)
return;

Expand Down Expand Up @@ -110,7 +110,7 @@ void SuspiciousMemsetUsageCheck::check(const MatchFinder::MatchResult &Result) {
Expr::EvalResult EVResult;
if (!FillChar->isValueDependent() &&
FillChar->EvaluateAsInt(EVResult, *Result.Context)) {
llvm::APSInt Value1 = EVResult.Val.getInt();
const llvm::APSInt Value1 = EVResult.Val.getInt();
if (Value1 == 0 || Value1.isNegative())
return;
}
Expand All @@ -120,8 +120,10 @@ void SuspiciousMemsetUsageCheck::check(const MatchFinder::MatchResult &Result) {
// and fix-its to swap the arguments.
auto D = diag(Call->getBeginLoc(),
"memset of size zero, potentially swapped arguments");
StringRef RHSString = tooling::fixit::getText(*ByteCount, *Result.Context);
StringRef LHSString = tooling::fixit::getText(*FillChar, *Result.Context);
const StringRef RHSString =
tooling::fixit::getText(*ByteCount, *Result.Context);
const StringRef LHSString =
tooling::fixit::getText(*FillChar, *Result.Context);
if (LHSString.empty() || RHSString.empty())
return;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ static bool isConcatenatedLiteralsOnPurpose(ASTContext *Ctx,
// String literals surrounded by parentheses are assumed to be on purpose.
// i.e.: const char* Array[] = { ("a" "b" "c"), "d", [...] };

TraversalKindScope RAII(*Ctx, TK_AsIs);
const TraversalKindScope RAII(*Ctx, TK_AsIs);
auto Parents = Ctx->getParents(*Lit);
if (Parents.size() == 1 && Parents[0].get<ParenExpr>() != nullptr)
return true;
Expand All @@ -35,15 +35,15 @@ static bool isConcatenatedLiteralsOnPurpose(ASTContext *Ctx,
// };
const SourceManager &SM = Ctx->getSourceManager();
bool IndentedCorrectly = true;
SourceLocation FirstToken = Lit->getStrTokenLoc(0);
FileID BaseFID = SM.getFileID(FirstToken);
unsigned int BaseIndent = SM.getSpellingColumnNumber(FirstToken);
unsigned int BaseLine = SM.getSpellingLineNumber(FirstToken);
const SourceLocation FirstToken = Lit->getStrTokenLoc(0);
const FileID BaseFID = SM.getFileID(FirstToken);
const unsigned int BaseIndent = SM.getSpellingColumnNumber(FirstToken);
const unsigned int BaseLine = SM.getSpellingLineNumber(FirstToken);
for (unsigned int TokNum = 1; TokNum < Lit->getNumConcatenated(); ++TokNum) {
SourceLocation Token = Lit->getStrTokenLoc(TokNum);
FileID FID = SM.getFileID(Token);
unsigned int Indent = SM.getSpellingColumnNumber(Token);
unsigned int Line = SM.getSpellingLineNumber(Token);
const SourceLocation Token = Lit->getStrTokenLoc(TokNum);
const FileID FID = SM.getFileID(Token);
const unsigned int Indent = SM.getSpellingColumnNumber(Token);
const unsigned int Line = SM.getSpellingLineNumber(Token);
if (FID != BaseFID || Line != BaseLine + TokNum || Indent <= BaseIndent) {
IndentedCorrectly = false;
break;
Expand Down Expand Up @@ -100,7 +100,7 @@ void SuspiciousMissingCommaCheck::check(
assert(InitializerList && ConcatenatedLiteral);

// Skip small arrays as they often generate false-positive.
unsigned int Size = InitializerList->getNumInits();
const unsigned int Size = InitializerList->getNumInits();
if (Size < SizeThreshold)
return;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class IsSamePtrExpr : public StmtVisitor<IsSamePtrExpr, bool> {
return false;
if (!check(E1->getBase(), E2->getBase()))
return false;
DeclAccessPair FD = E1->getFoundDecl();
const DeclAccessPair FD = E1->getFoundDecl();
return isa<FieldDecl>(FD.getDecl()) && FD == E2->getFoundDecl();
}

Expand Down Expand Up @@ -145,7 +145,7 @@ void SuspiciousReallocUsageCheck::check(
if (FindAssignToVarBefore{Var, DeclRef, SM}.Visit(Func->getBody()))
return;

StringRef CodeOfAssignedExpr = Lexer::getSourceText(
const StringRef CodeOfAssignedExpr = Lexer::getSourceText(
CharSourceRange::getTokenRange(PtrResultExpr->getSourceRange()), SM,
getLangOpts());
diag(Call->getBeginLoc(), "'%0' may be set to null if 'realloc' fails, which "
Expand Down
18 changes: 10 additions & 8 deletions clang-tools-extra/clang-tidy/bugprone/SuspiciousSemicolonCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void SuspiciousSemicolonCheck::check(const MatchFinder::MatchResult &Result) {
return;

const auto *Semicolon = Result.Nodes.getNodeAs<NullStmt>("semi");
SourceLocation LocStart = Semicolon->getBeginLoc();
const SourceLocation LocStart = Semicolon->getBeginLoc();

if (LocStart.isMacroID())
return;
Expand All @@ -40,7 +40,7 @@ void SuspiciousSemicolonCheck::check(const MatchFinder::MatchResult &Result) {
auto Token = utils::lexer::getPreviousToken(LocStart, Ctxt.getSourceManager(),
Ctxt.getLangOpts());
auto &SM = *Result.SourceManager;
unsigned SemicolonLine = SM.getSpellingLineNumber(LocStart);
const unsigned SemicolonLine = SM.getSpellingLineNumber(LocStart);

const auto *Statement = Result.Nodes.getNodeAs<Stmt>("stmt");
const bool IsIfStmt = isa<IfStmt>(Statement);
Expand All @@ -49,18 +49,20 @@ void SuspiciousSemicolonCheck::check(const MatchFinder::MatchResult &Result) {
SM.getSpellingLineNumber(Token.getLocation()) != SemicolonLine)
return;

SourceLocation LocEnd = Semicolon->getEndLoc();
FileID FID = SM.getFileID(LocEnd);
llvm::MemoryBufferRef Buffer = SM.getBufferOrFake(FID, LocEnd);
const SourceLocation LocEnd = Semicolon->getEndLoc();
const FileID FID = SM.getFileID(LocEnd);
const llvm::MemoryBufferRef Buffer = SM.getBufferOrFake(FID, LocEnd);
Lexer Lexer(SM.getLocForStartOfFile(FID), Ctxt.getLangOpts(),
Buffer.getBufferStart(), SM.getCharacterData(LocEnd) + 1,
Buffer.getBufferEnd());
if (Lexer.LexFromRawLexer(Token))
return;

unsigned BaseIndent = SM.getSpellingColumnNumber(Statement->getBeginLoc());
unsigned NewTokenIndent = SM.getSpellingColumnNumber(Token.getLocation());
unsigned NewTokenLine = SM.getSpellingLineNumber(Token.getLocation());
const unsigned BaseIndent =
SM.getSpellingColumnNumber(Statement->getBeginLoc());
const unsigned NewTokenIndent =
SM.getSpellingColumnNumber(Token.getLocation());
const unsigned NewTokenLine = SM.getSpellingLineNumber(Token.getLocation());

if (!IsIfStmt && NewTokenIndent <= BaseIndent &&
Token.getKind() != tok::l_brace && NewTokenLine != SemicolonLine)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ void SuspiciousStringCompareCheck::registerMatchers(MatchFinder *Finder) {

// Add the list of known string compare-like functions and add user-defined
// functions.
std::vector<StringRef> FunctionNames = utils::options::parseListPair(
const std::vector<StringRef> FunctionNames = utils::options::parseListPair(
KnownStringCompareFunctions, StringCompareLikeFunctions);

// Match a call to a string compare functions.
Expand Down Expand Up @@ -163,7 +163,7 @@ void SuspiciousStringCompareCheck::check(
assert(Decl != nullptr && Call != nullptr);

if (Result.Nodes.getNodeAs<Stmt>("missing-comparison")) {
SourceLocation EndLoc = Lexer::getLocForEndOfToken(
const SourceLocation EndLoc = Lexer::getLocForEndOfToken(
Call->getRParenLoc(), 0, Result.Context->getSourceManager(),
getLangOpts());

Expand All @@ -173,10 +173,10 @@ void SuspiciousStringCompareCheck::check(
}

if (const auto *E = Result.Nodes.getNodeAs<Expr>("logical-not-comparison")) {
SourceLocation EndLoc = Lexer::getLocForEndOfToken(
const SourceLocation EndLoc = Lexer::getLocForEndOfToken(
Call->getRParenLoc(), 0, Result.Context->getSourceManager(),
getLangOpts());
SourceLocation NotLoc = E->getBeginLoc();
const SourceLocation NotLoc = E->getBeginLoc();

diag(Call->getBeginLoc(),
"function %0 is compared using logical not operator")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ static bool areArgumentsPotentiallySwapped(const QualType LTo,
if (LTo == RFrom && REq)
return true;

bool LEq = areTypesSemiEqual(LTo, RFrom);
const bool LEq = areTypesSemiEqual(LTo, RFrom);
if (RTo == LFrom && LEq)
return true;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void ThrowingStaticInitializationCheck::check(
"duration may throw an exception that cannot be caught")
<< VD << (VD->getStorageDuration() == SD_Static ? 0 : 1);

SourceLocation FuncLocation = Func->getLocation();
const SourceLocation FuncLocation = Func->getLocation();
if (FuncLocation.isValid()) {
diag(FuncLocation,
"possibly throwing %select{constructor|function}0 declared here",
Expand Down
32 changes: 16 additions & 16 deletions clang-tools-extra/clang-tidy/bugprone/TooSmallLoopVariableCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,21 @@ void TooSmallLoopVariableCheck::storeOptions(
/// LoopName: The entire for loop (as a ForStmt)
///
void TooSmallLoopVariableCheck::registerMatchers(MatchFinder *Finder) {
StatementMatcher LoopVarMatcher =
const StatementMatcher LoopVarMatcher =
expr(ignoringParenImpCasts(
anyOf(declRefExpr(to(varDecl(hasType(isInteger())))),
memberExpr(member(fieldDecl(hasType(isInteger())))))))
.bind(LoopVarName);

// We need to catch only those comparisons which contain any integer cast.
StatementMatcher LoopVarConversionMatcher = traverse(
const StatementMatcher LoopVarConversionMatcher = traverse(
TK_AsIs, implicitCastExpr(hasImplicitDestinationType(isInteger()),
has(ignoringParenImpCasts(LoopVarMatcher)))
.bind(LoopVarCastName));

// We are interested in only those cases when the loop bound is a variable
// value (not const, enum, etc.).
StatementMatcher LoopBoundMatcher =
const StatementMatcher LoopBoundMatcher =
expr(ignoringParenImpCasts(allOf(
hasType(isInteger()), unless(integerLiteral()),
unless(allOf(
Expand All @@ -94,7 +94,7 @@ void TooSmallLoopVariableCheck::registerMatchers(MatchFinder *Finder) {

// We use the loop increment expression only to make sure we found the right
// loop variable.
StatementMatcher IncrementMatcher =
const StatementMatcher IncrementMatcher =
expr(ignoringParenImpCasts(hasType(isInteger()))).bind(LoopIncrementName);

Finder->addMatcher(
Expand All @@ -121,14 +121,14 @@ static MagnitudeBits calcMagnitudeBits(const ASTContext &Context,
const Expr *IntExpr) {
assert(IntExprType->isIntegerType());

unsigned SignedBits = IntExprType->isUnsignedIntegerType() ? 0U : 1U;
const unsigned SignedBits = IntExprType->isUnsignedIntegerType() ? 0U : 1U;

if (const auto *BitField = IntExpr->getSourceBitField()) {
unsigned BitFieldWidth = BitField->getBitWidthValue();
const unsigned BitFieldWidth = BitField->getBitWidthValue();
return {BitFieldWidth - SignedBits, BitFieldWidth};
}

unsigned IntWidth = Context.getIntWidth(IntExprType);
const unsigned IntWidth = Context.getIntWidth(IntExprType);
return {IntWidth - SignedBits, 0U};
}

Expand All @@ -143,18 +143,18 @@ calcUpperBoundMagnitudeBits(const ASTContext &Context, const Expr *UpperBound,
const Expr *RHSE = BinOperator->getRHS()->IgnoreParenImpCasts();
const Expr *LHSE = BinOperator->getLHS()->IgnoreParenImpCasts();

QualType RHSEType = RHSE->getType();
QualType LHSEType = LHSE->getType();
const QualType RHSEType = RHSE->getType();
const QualType LHSEType = LHSE->getType();

if (!RHSEType->isIntegerType() || !LHSEType->isIntegerType())
return {};

bool RHSEIsConstantValue = RHSEType->isEnumeralType() ||
RHSEType.isConstQualified() ||
isa<IntegerLiteral>(RHSE);
bool LHSEIsConstantValue = LHSEType->isEnumeralType() ||
LHSEType.isConstQualified() ||
isa<IntegerLiteral>(LHSE);
const bool RHSEIsConstantValue = RHSEType->isEnumeralType() ||
RHSEType.isConstQualified() ||
isa<IntegerLiteral>(RHSE);
const bool LHSEIsConstantValue = LHSEType->isEnumeralType() ||
LHSEType.isConstQualified() ||
isa<IntegerLiteral>(LHSE);

// Avoid false positives produced by two constant values.
if (RHSEIsConstantValue && LHSEIsConstantValue)
Expand Down Expand Up @@ -193,7 +193,7 @@ void TooSmallLoopVariableCheck::check(const MatchFinder::MatchResult &Result) {
if (LoopVar->getType() != LoopIncrement->getType())
return;

ASTContext &Context = *Result.Context;
const ASTContext &Context = *Result.Context;

const QualType LoopVarType = LoopVar->getType();
const MagnitudeBits LoopVarMagnitudeBits =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ static ConversionKind classifyFormatString(StringRef Fmt, const LangOptions &LO,

// Get the conversion specifier and use it to determine the conversion
// kind.
analyze_scanf::ScanfConversionSpecifier SCS = FS.getConversionSpecifier();
const analyze_scanf::ScanfConversionSpecifier SCS =
FS.getConversionSpecifier();
if (SCS.isIntArg()) {
switch (FS.getLengthModifier().getKind()) {
case analyze_scanf::LengthModifier::AsLongLong:
Expand Down Expand Up @@ -194,7 +195,7 @@ void UncheckedStringToNumberConversionCheck::check(
// The format string comes from the call expression and depends on which
// flavor of scanf is called.
// Index 0: scanf, vscanf, Index 1: fscanf, sscanf, vfscanf, vsscanf.
unsigned Idx =
const unsigned Idx =
(FFD->getName() == "scanf" || FFD->getName() == "vscanf") ? 0 : 1;

// Given the index, see if the call expression argument at that index is
Expand Down
Loading