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 @@ -79,7 +79,7 @@ void NamedParameterCheck::check(const MatchFinder::MatchResult &Result) {
// void foo(int /*unused*/)
const char *Begin = SM.getCharacterData(Parm->getBeginLoc());
const char *End = SM.getCharacterData(Parm->getLocation());
StringRef Data(Begin, End - Begin);
const StringRef Data(Begin, End - Begin);
if (Data.contains("/*"))
continue;

Expand All @@ -104,15 +104,15 @@ void NamedParameterCheck::check(const MatchFinder::MatchResult &Result) {
if (M && M->size_overridden_methods() > 0) {
const ParmVarDecl *OtherParm =
(*M->begin_overridden_methods())->getParamDecl(P.second);
StringRef Name = OtherParm->getName();
const StringRef Name = OtherParm->getName();
if (!Name.empty())
NewName = Name;
}

// If the definition has a named parameter use that name.
if (Definition) {
const ParmVarDecl *DefParm = Definition->getParamDecl(P.second);
StringRef Name = DefParm->getName();
const StringRef Name = DefParm->getName();
if (!Name.empty())
NewName = Name;
}
Expand Down
22 changes: 12 additions & 10 deletions clang-tools-extra/clang-tidy/readability/NamespaceCommentCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ getNamespaceNameAsWritten(SourceLocation &Loc, const SourceManager &Sources,
--Nesting;
} else if (Nesting == 0) {
if (T->is(tok::raw_identifier)) {
StringRef ID = T->getRawIdentifier();
const StringRef ID = T->getRawIdentifier();
if (ID != "namespace")
Result.append(std::string(ID));
if (ID == "inline")
Expand All @@ -96,13 +96,13 @@ void NamespaceCommentCheck::check(const MatchFinder::MatchResult &Result) {

// Don't require closing comments for namespaces spanning less than certain
// number of lines.
unsigned StartLine = Sources.getSpellingLineNumber(ND->getBeginLoc());
unsigned EndLine = Sources.getSpellingLineNumber(ND->getRBraceLoc());
const unsigned StartLine = Sources.getSpellingLineNumber(ND->getBeginLoc());
const unsigned EndLine = Sources.getSpellingLineNumber(ND->getRBraceLoc());
if (EndLine - StartLine + 1 <= ShortNamespaceLines)
return;

// Find next token after the namespace closing brace.
SourceLocation AfterRBrace = Lexer::getLocForEndOfToken(
const SourceLocation AfterRBrace = Lexer::getLocForEndOfToken(
ND->getRBraceLoc(), /*Offset=*/0, Sources, getLangOpts());
SourceLocation Loc = AfterRBrace;
SourceLocation LBraceLoc = ND->getBeginLoc();
Expand Down Expand Up @@ -137,7 +137,8 @@ void NamespaceCommentCheck::check(const MatchFinder::MatchResult &Result) {
if (!locationsInSameFile(Sources, ND->getRBraceLoc(), Loc))
return;

bool NextTokenIsOnSameLine = Sources.getSpellingLineNumber(Loc) == EndLine;
const bool NextTokenIsOnSameLine =
Sources.getSpellingLineNumber(Loc) == EndLine;
// If we insert a line comment before the token in the same line, we need
// to insert a line break.
bool NeedLineBreak = NextTokenIsOnSameLine && Tok.isNot(tok::eof);
Expand All @@ -148,11 +149,12 @@ void NamespaceCommentCheck::check(const MatchFinder::MatchResult &Result) {

// Try to find existing namespace closing comment on the same line.
if (Tok.is(tok::comment) && NextTokenIsOnSameLine) {
StringRef Comment(Sources.getCharacterData(Loc), Tok.getLength());
const StringRef Comment(Sources.getCharacterData(Loc), Tok.getLength());
SmallVector<StringRef, 7> Groups;
if (NamespaceCommentPattern.match(Comment, &Groups)) {
StringRef NamespaceNameInComment = Groups.size() > 5 ? Groups[5] : "";
StringRef Anonymous = Groups.size() > 3 ? Groups[3] : "";
const StringRef NamespaceNameInComment =
Groups.size() > 5 ? Groups[5] : "";
const StringRef Anonymous = Groups.size() > 3 ? Groups[3] : "";

if ((ND->isAnonymousNamespace() && NamespaceNameInComment.empty()) ||
(*NamespaceNameAsWritten == NamespaceNameInComment &&
Expand Down Expand Up @@ -186,7 +188,7 @@ void NamespaceCommentCheck::check(const MatchFinder::MatchResult &Result) {
// multi-line or there may be other tokens behind it.
}

std::string NamespaceNameForDiag =
const std::string NamespaceNameForDiag =
ND->isAnonymousNamespace()
? "anonymous namespace"
: ("namespace '" + *NamespaceNameAsWritten + "'");
Expand All @@ -203,7 +205,7 @@ void NamespaceCommentCheck::check(const MatchFinder::MatchResult &Result) {
Fix.append("\n");

// Place diagnostic at an old comment, or closing brace if we did not have it.
SourceLocation DiagLoc =
const SourceLocation DiagLoc =
OldCommentRange.getBegin() != OldCommentRange.getEnd()
? OldCommentRange.getBegin()
: ND->getRBraceLoc();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ void NonConstParameterCheck::diagnoseNonConstParameters() {
dyn_cast_or_null<const FunctionDecl>(Par->getParentFunctionOrMethod());
if (!Function)
continue;
unsigned Index = Par->getFunctionScopeIndex();
const unsigned Index = Par->getFunctionScopeIndex();
for (FunctionDecl *FnDecl : Function->redecls()) {
if (FnDecl->getNumParams() <= Index)
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ static StringRef getOperatorSpelling(SourceLocation Loc, ASTContext &Context) {
if (Loc.isInvalid())
return {};

SourceManager &SM = Context.getSourceManager();
const SourceManager &SM = Context.getSourceManager();

Loc = SM.getSpellingLoc(Loc);
if (Loc.isInvalid())
Expand All @@ -41,7 +41,7 @@ AST_MATCHER_P2(BinaryOperator, hasInvalidBinaryOperatorRepresentation,
if (Node.getOpcode() != Kind || ExpectedRepresentation.empty())
return false;

StringRef Spelling =
const StringRef Spelling =
getOperatorSpelling(Node.getOperatorLoc(), Finder->getASTContext());
return !Spelling.empty() && Spelling != ExpectedRepresentation;
}
Expand All @@ -52,7 +52,7 @@ AST_MATCHER_P2(UnaryOperator, hasInvalidUnaryOperatorRepresentation,
if (Node.getOpcode() != Kind || ExpectedRepresentation.empty())
return false;

StringRef Spelling =
const StringRef Spelling =
getOperatorSpelling(Node.getOperatorLoc(), Finder->getASTContext());
return !Spelling.empty() && Spelling != ExpectedRepresentation;
}
Expand All @@ -63,7 +63,7 @@ AST_MATCHER_P2(CXXOperatorCallExpr, hasInvalidOverloadedOperatorRepresentation,
if (Node.getOperator() != Kind || ExpectedRepresentation.empty())
return false;

StringRef Spelling =
const StringRef Spelling =
getOperatorSpelling(Node.getOperatorLoc(), Finder->getASTContext());
return !Spelling.empty() && Spelling != ExpectedRepresentation;
}
Expand Down Expand Up @@ -297,9 +297,9 @@ void OperatorsRepresentationCheck::check(
if (TokenRange.isInvalid())
return;

StringRef Spelling = Lexer::getSourceText(TokenRange, *Result.SourceManager,
Result.Context->getLangOpts());
StringRef TranslatedSpelling = translate(Spelling);
const StringRef Spelling = Lexer::getSourceText(
TokenRange, *Result.SourceManager, Result.Context->getLangOpts());
const StringRef TranslatedSpelling = translate(Spelling);

if (TranslatedSpelling.empty())
return;
Expand All @@ -312,7 +312,7 @@ void OperatorsRepresentationCheck::check(
SourceRepresentation = "a traditional";
TargetRepresentation = "an alternative";

StringRef SpellingEx = Lexer::getSourceText(
const StringRef SpellingEx = Lexer::getSourceText(
CharSourceRange::getCharRange(
TokenRange.getBegin().getLocWithOffset(-1),
TokenRange.getBegin().getLocWithOffset(Spelling.size() + 1U)),
Expand Down
38 changes: 19 additions & 19 deletions clang-tools-extra/clang-tidy/readability/QualifiedAutoCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,18 @@ findQualToken(const VarDecl *Decl, Qualifier Qual,
SourceLocation BeginLoc = Decl->getQualifierLoc().getBeginLoc();
if (BeginLoc.isInvalid())
BeginLoc = Decl->getBeginLoc();
SourceLocation EndLoc = Decl->getLocation();
const SourceLocation EndLoc = Decl->getLocation();

CharSourceRange FileRange = Lexer::makeFileCharRange(
const CharSourceRange FileRange = Lexer::makeFileCharRange(
CharSourceRange::getCharRange(BeginLoc, EndLoc), *Result.SourceManager,
Result.Context->getLangOpts());

if (FileRange.isInvalid())
return std::nullopt;

tok::TokenKind Tok = Qual == Qualifier::Const ? tok::kw_const
: Qual == Qualifier::Volatile ? tok::kw_volatile
: tok::kw_restrict;
const tok::TokenKind Tok = Qual == Qualifier::Const ? tok::kw_const
: Qual == Qualifier::Volatile ? tok::kw_volatile
: tok::kw_restrict;

return utils::lexer::getQualifyingToken(Tok, FileRange, *Result.Context,
*Result.SourceManager);
Expand Down Expand Up @@ -90,13 +90,13 @@ mergeReplacementRange(SourceRange &TypeSpecifier, const Token &ConstToken) {
}

static bool isPointerConst(QualType QType) {
QualType Pointee = QType->getPointeeType();
const QualType Pointee = QType->getPointeeType();
assert(!Pointee.isNull() && "can't have a null Pointee");
return Pointee.isConstQualified();
}

static bool isAutoPointerConst(QualType QType) {
QualType Pointee =
const QualType Pointee =
cast<AutoType>(QType->getPointeeType().getTypePtr())->desugar();
assert(!Pointee.isNull() && "can't have a null Pointee");
return Pointee.isConstQualified();
Expand Down Expand Up @@ -146,7 +146,6 @@ void QualifiedAutoCheck::registerMatchers(MatchFinder *Finder) {
return qualType(anyOf(qualType(pointerType(pointee(InnerMatchers...))),
qualType(substTemplateTypeParmType(hasReplacementType(
pointerType(pointee(InnerMatchers...)))))));

};

auto IsAutoDeducedToPointer =
Expand Down Expand Up @@ -223,33 +222,34 @@ void QualifiedAutoCheck::check(const MatchFinder::MatchResult &Result) {
if (Var->getLocation() == TypeSpecifier.getEnd().getLocWithOffset(1))
TypeSpecifier.setEnd(TypeSpecifier.getEnd().getLocWithOffset(1));

CharSourceRange FixItRange = CharSourceRange::getCharRange(TypeSpecifier);
const CharSourceRange FixItRange =
CharSourceRange::getCharRange(TypeSpecifier);
if (FixItRange.isInvalid())
return;

SourceLocation FixitLoc = FixItRange.getBegin();
for (SourceRange &Range : RemoveQualifiersRange) {
for (const SourceRange &Range : RemoveQualifiersRange) {
if (Range.getBegin() < FixitLoc)
FixitLoc = Range.getBegin();
}

std::string ReplStr = [&] {
llvm::StringRef PtrConst = isPointerConst(Var->getType()) ? "const " : "";
llvm::StringRef LocalConst = IsLocalConst ? "const " : "";
llvm::StringRef LocalVol = IsLocalVolatile ? "volatile " : "";
llvm::StringRef LocalRestrict = IsLocalRestrict ? "__restrict " : "";
const std::string ReplStr = [&] {
const StringRef PtrConst = isPointerConst(Var->getType()) ? "const " : "";
const StringRef LocalConst = IsLocalConst ? "const " : "";
const StringRef LocalVol = IsLocalVolatile ? "volatile " : "";
const StringRef LocalRestrict = IsLocalRestrict ? "__restrict " : "";
return (PtrConst + "auto *" + LocalConst + LocalVol + LocalRestrict)
.str();
}();

DiagnosticBuilder Diag =
const DiagnosticBuilder Diag =
diag(FixitLoc,
"'%select{|const }0%select{|volatile }1%select{|__restrict }2auto "
"%3' can be declared as '%4%3'")
<< IsLocalConst << IsLocalVolatile << IsLocalRestrict << Var->getName()
<< ReplStr;

for (SourceRange &Range : RemoveQualifiersRange) {
for (const SourceRange &Range : RemoveQualifiersRange) {
Diag << FixItHint::CreateRemoval(CharSourceRange::getCharRange(Range));
}

Expand Down Expand Up @@ -286,7 +286,7 @@ void QualifiedAutoCheck::check(const MatchFinder::MatchResult &Result) {
if (TypeSpec->isInvalid() || TypeSpec->getBegin().isMacroID() ||
TypeSpec->getEnd().isMacroID())
return;
SourceLocation InsertPos = TypeSpec->getBegin();
const SourceLocation InsertPos = TypeSpec->getBegin();
diag(InsertPos,
"'auto *%select{|const }0%select{|volatile }1%2' can be declared as "
"'const auto *%select{|const }0%select{|volatile }1%2'")
Expand All @@ -308,7 +308,7 @@ void QualifiedAutoCheck::check(const MatchFinder::MatchResult &Result) {
if (TypeSpec->isInvalid() || TypeSpec->getBegin().isMacroID() ||
TypeSpec->getEnd().isMacroID())
return;
SourceLocation InsertPos = TypeSpec->getBegin();
const SourceLocation InsertPos = TypeSpec->getBegin();
diag(InsertPos, "'auto &%0' can be declared as 'const auto &%0'")
<< Var->getName() << FixItHint::CreateInsertion(InsertPos, "const ");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ void RedundantAccessSpecifiersCheck::check(
LastASDecl = ASDecl;

if (CheckFirstDeclaration) {
AccessSpecifier DefaultSpecifier =
const AccessSpecifier DefaultSpecifier =
MatchedDecl->isClass() ? AS_private : AS_public;
if (ASDecl->getAccess() == DefaultSpecifier) {
diag(ASDecl->getLocation(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ static bool areTypesEqual(QualType S, QualType D) {
if (TS != TD)
return false;

QualType PtrS = S->getPointeeType();
QualType PtrD = D->getPointeeType();
const QualType PtrS = S->getPointeeType();
const QualType PtrD = D->getPointeeType();

if (!PtrS.isNull() && !PtrD.isNull())
return areTypesEqual(PtrS, PtrD);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ void RedundantControlFlowCheck::check(const MatchFinder::MatchResult &Result) {

void RedundantControlFlowCheck::checkRedundantReturn(
const MatchFinder::MatchResult &Result, const CompoundStmt *Block) {
CompoundStmt::const_reverse_body_iterator Last = Block->body_rbegin();
const CompoundStmt::const_reverse_body_iterator Last = Block->body_rbegin();
if (const auto *Return = dyn_cast<ReturnStmt>(*Last))
issueDiagnostic(Result, Block, Return->getSourceRange(),
RedundantReturnDiag);
}

void RedundantControlFlowCheck::checkRedundantContinue(
const MatchFinder::MatchResult &Result, const CompoundStmt *Block) {
CompoundStmt::const_reverse_body_iterator Last = Block->body_rbegin();
const CompoundStmt::const_reverse_body_iterator Last = Block->body_rbegin();
if (const auto *Continue = dyn_cast<ContinueStmt>(*Last))
issueDiagnostic(Result, Block, Continue->getSourceRange(),
RedundantContinueDiag);
Expand All @@ -67,11 +67,12 @@ void RedundantControlFlowCheck::checkRedundantContinue(
void RedundantControlFlowCheck::issueDiagnostic(
const MatchFinder::MatchResult &Result, const CompoundStmt *const Block,
const SourceRange &StmtRange, const char *const Diag) {
SourceManager &SM = *Result.SourceManager;
const SourceManager &SM = *Result.SourceManager;
if (isLocationInMacroExpansion(SM, StmtRange.getBegin()))
return;

CompoundStmt::const_reverse_body_iterator Previous = ++Block->body_rbegin();
const CompoundStmt::const_reverse_body_iterator Previous =
++Block->body_rbegin();
SourceLocation Start;
if (Previous != Block->body_rend())
Start = Lexer::findLocationAfterToken(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ void RedundantDeclarationCheck::check(const MatchFinder::MatchResult &Result) {
}
}

SourceLocation EndLoc = Lexer::getLocForEndOfToken(
const SourceLocation EndLoc = Lexer::getLocForEndOfToken(
D->getSourceRange().getEnd(), 0, SM, Result.Context->getLangOpts());
{
auto Diag = diag(D->getLocation(), "redundant %0 declaration") << D;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ AST_POLYMORPHIC_MATCHER_P(isInternalLinkage,
static SourceLocation getInlineTokenLocation(SourceRange RangeLocation,
const SourceManager &Sources,
const LangOptions &LangOpts) {
SourceLocation Loc = RangeLocation.getBegin();
const SourceLocation Loc = RangeLocation.getBegin();
if (Loc.isMacroID())
return {};

Expand Down Expand Up @@ -106,7 +106,7 @@ template <typename T>
void RedundantInlineSpecifierCheck::handleMatchedDecl(
const T *MatchedDecl, const SourceManager &Sources,
const MatchFinder::MatchResult &Result, StringRef Message) {
SourceLocation Loc = getInlineTokenLocation(
const SourceLocation Loc = getInlineTokenLocation(
MatchedDecl->getSourceRange(), Sources, Result.Context->getLangOpts());
if (Loc.isValid())
diag(Loc, Message) << MatchedDecl << FixItHint::CreateRemoval(Loc);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,23 @@ class RedundantPreprocessorCallbacks : public PPCallbacks {

void If(SourceLocation Loc, SourceRange ConditionRange,
ConditionValueKind ConditionValue) override {
StringRef Condition =
const StringRef Condition =
Lexer::getSourceText(CharSourceRange::getTokenRange(ConditionRange),
PP.getSourceManager(), PP.getLangOpts());
checkMacroRedundancy(Loc, Condition, IfStack, DK_If, DK_If, true);
}

void Ifdef(SourceLocation Loc, const Token &MacroNameTok,
const MacroDefinition &MacroDefinition) override {
std::string MacroName = PP.getSpelling(MacroNameTok);
const std::string MacroName = PP.getSpelling(MacroNameTok);
checkMacroRedundancy(Loc, MacroName, IfdefStack, DK_Ifdef, DK_Ifdef, true);
checkMacroRedundancy(Loc, MacroName, IfndefStack, DK_Ifdef, DK_Ifndef,
false);
}

void Ifndef(SourceLocation Loc, const Token &MacroNameTok,
const MacroDefinition &MacroDefinition) override {
std::string MacroName = PP.getSpelling(MacroNameTok);
const std::string MacroName = PP.getSpelling(MacroNameTok);
checkMacroRedundancy(Loc, MacroName, IfndefStack, DK_Ifndef, DK_Ifndef,
true);
checkMacroRedundancy(Loc, MacroName, IfdefStack, DK_Ifndef, DK_Ifdef,
Expand Down
Loading