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 @@ -45,7 +45,10 @@ struct StrCatCheckResult {
std::vector<FixItHint> Hints;
};

void removeCallLeaveArgs(const CallExpr *Call, StrCatCheckResult *CheckResult) {
} // namespace

static void removeCallLeaveArgs(const CallExpr *Call,
StrCatCheckResult *CheckResult) {
if (Call->getNumArgs() == 0)
return;
// Remove 'Foo('
Expand All @@ -58,9 +61,9 @@ void removeCallLeaveArgs(const CallExpr *Call, StrCatCheckResult *CheckResult) {
Call->getRParenLoc(), Call->getEndLoc().getLocWithOffset(1))));
}

const clang::CallExpr *processArgument(const Expr *Arg,
const MatchFinder::MatchResult &Result,
StrCatCheckResult *CheckResult) {
static const clang::CallExpr *
processArgument(const Expr *Arg, const MatchFinder::MatchResult &Result,
StrCatCheckResult *CheckResult) {
const auto IsAlphanum = hasDeclaration(cxxMethodDecl(hasName("AlphaNum")));
static const auto *const Strcat = new auto(hasName("::absl::StrCat"));
const auto IsStrcat = cxxBindTemporaryExpr(
Expand All @@ -78,8 +81,8 @@ const clang::CallExpr *processArgument(const Expr *Arg,
return nullptr;
}

StrCatCheckResult processCall(const CallExpr *RootCall, bool IsAppend,
const MatchFinder::MatchResult &Result) {
static StrCatCheckResult processCall(const CallExpr *RootCall, bool IsAppend,
const MatchFinder::MatchResult &Result) {
StrCatCheckResult CheckResult;
std::deque<const CallExpr *> CallsToProcess = {RootCall};

Expand All @@ -101,7 +104,6 @@ StrCatCheckResult processCall(const CallExpr *RootCall, bool IsAppend,
}
return CheckResult;
}
} // namespace

void RedundantStrcatCallsCheck::check(const MatchFinder::MatchResult &Result) {
bool IsAppend = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ namespace {

AST_MATCHER(Expr, isInMacro) { return Node.getBeginLoc().isMacroID(); }

} // namespace

/// Find the next statement after `S`.
const Stmt *nextStmt(const MatchFinder::MatchResult &Result, const Stmt *S) {
static const Stmt *nextStmt(const MatchFinder::MatchResult &Result,
const Stmt *S) {
auto Parents = Result.Context->getParents(*S);
if (Parents.empty())
return nullptr;
Expand All @@ -40,8 +43,8 @@ using ExpansionRanges = std::vector<SourceRange>;
/// \brief Get all the macro expansion ranges related to `Loc`.
///
/// The result is ordered from most inner to most outer.
ExpansionRanges getExpansionRanges(SourceLocation Loc,
const MatchFinder::MatchResult &Result) {
static ExpansionRanges
getExpansionRanges(SourceLocation Loc, const MatchFinder::MatchResult &Result) {
ExpansionRanges Locs;
while (Loc.isMacroID()) {
Locs.push_back(
Expand All @@ -51,8 +54,6 @@ ExpansionRanges getExpansionRanges(SourceLocation Loc,
return Locs;
}

} // namespace

void MultipleStatementMacroCheck::registerMatchers(MatchFinder *Finder) {
const auto Inner = expr(isInMacro(), unless(compoundStmt())).bind("inner");
Finder->addMatcher(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@ AST_MATCHER(CXXRecordDecl, hasDefaultConstructor) {
return Node.hasDefaultConstructor();
}

} // namespace

// Iterate over all the fields in a record type, both direct and indirect (e.g.
// if the record contains an anonymous struct).
template <typename T, typename Func>
void forEachField(const RecordDecl &Record, const T &Fields, const Func &Fn) {
static void forEachField(const RecordDecl &Record, const T &Fields,
const Func &Fn) {
for (const FieldDecl *F : Fields) {
if (F->isAnonymousStructOrUnion()) {
if (const CXXRecordDecl *R = F->getType()->getAsCXXRecordDecl())
Expand All @@ -43,8 +46,9 @@ void forEachField(const RecordDecl &Record, const T &Fields, const Func &Fn) {
}

template <typename T, typename Func>
void forEachFieldWithFilter(const RecordDecl &Record, const T &Fields,
bool &AnyMemberHasInitPerUnion, const Func &Fn) {
static void forEachFieldWithFilter(const RecordDecl &Record, const T &Fields,
bool &AnyMemberHasInitPerUnion,
const Func &Fn) {
for (const FieldDecl *F : Fields) {
if (F->isAnonymousStructOrUnion()) {
if (const CXXRecordDecl *R = F->getType()->getAsCXXRecordDecl()) {
Expand All @@ -59,8 +63,9 @@ void forEachFieldWithFilter(const RecordDecl &Record, const T &Fields,
}
}

void removeFieldInitialized(const FieldDecl *M,
SmallPtrSetImpl<const FieldDecl *> &FieldDecls) {
static void
removeFieldInitialized(const FieldDecl *M,
SmallPtrSetImpl<const FieldDecl *> &FieldDecls) {
const RecordDecl *R = M->getParent();
if (R && R->isUnion()) {
// Erase all members in a union if any member of it is initialized.
Expand All @@ -70,9 +75,9 @@ void removeFieldInitialized(const FieldDecl *M,
FieldDecls.erase(M);
}

void removeFieldsInitializedInBody(
const Stmt &Stmt, ASTContext &Context,
SmallPtrSetImpl<const FieldDecl *> &FieldDecls) {
static void
removeFieldsInitializedInBody(const Stmt &Stmt, ASTContext &Context,
SmallPtrSetImpl<const FieldDecl *> &FieldDecls) {
auto Matches =
match(findAll(binaryOperator(
hasOperatorName("="),
Expand All @@ -82,9 +87,9 @@ void removeFieldsInitializedInBody(
removeFieldInitialized(Match.getNodeAs<FieldDecl>("fieldDecl"), FieldDecls);
}

StringRef getName(const FieldDecl *Field) { return Field->getName(); }
static StringRef getName(const FieldDecl *Field) { return Field->getName(); }

StringRef getName(const RecordDecl *Record) {
static StringRef getName(const RecordDecl *Record) {
// Get the typedef name if this is a C-style anonymous struct and typedef.
if (const TypedefNameDecl *Typedef = Record->getTypedefNameForAnonDecl())
return Typedef->getName();
Expand All @@ -94,7 +99,7 @@ StringRef getName(const RecordDecl *Record) {
// Creates comma separated list of decls requiring initialization in order of
// declaration.
template <typename R, typename T>
std::string
static std::string
toCommaSeparatedString(const R &OrderedDecls,
const SmallPtrSetImpl<const T *> &DeclsToInit) {
SmallVector<StringRef, 16> Names;
Expand All @@ -105,12 +110,14 @@ toCommaSeparatedString(const R &OrderedDecls,
return llvm::join(Names.begin(), Names.end(), ", ");
}

SourceLocation getLocationForEndOfToken(const ASTContext &Context,
SourceLocation Location) {
static SourceLocation getLocationForEndOfToken(const ASTContext &Context,
SourceLocation Location) {
return Lexer::getLocForEndOfToken(Location, 0, Context.getSourceManager(),
Context.getLangOpts());
}

namespace {

// There are 3 kinds of insertion placements:
enum class InitializerPlacement {
// 1. The fields are inserted after an existing CXXCtorInitializer stored in
Expand Down Expand Up @@ -187,15 +194,17 @@ struct InitializerInsertion {
SmallVector<std::string, 4> Initializers;
};

} // namespace

// Convenience utility to get a RecordDecl from a QualType.
const RecordDecl *getCanonicalRecordDecl(const QualType &Type) {
static const RecordDecl *getCanonicalRecordDecl(const QualType &Type) {
if (const auto *RT = Type->getAsCanonical<RecordType>())
return RT->getDecl();
return nullptr;
}

template <typename R, typename T>
SmallVector<InitializerInsertion, 16>
static SmallVector<InitializerInsertion, 16>
computeInsertions(const CXXConstructorDecl::init_const_range &Inits,
const R &OrderedDecls,
const SmallPtrSetImpl<const T *> &DeclsToInit) {
Expand Down Expand Up @@ -239,8 +248,9 @@ computeInsertions(const CXXConstructorDecl::init_const_range &Inits,

// Gets the list of bases and members that could possibly be initialized, in
// order as they appear in the class declaration.
void getInitializationsInOrder(const CXXRecordDecl &ClassDecl,
SmallVectorImpl<const NamedDecl *> &Decls) {
static void
getInitializationsInOrder(const CXXRecordDecl &ClassDecl,
SmallVectorImpl<const NamedDecl *> &Decls) {
Decls.clear();
for (const auto &Base : ClassDecl.bases()) {
// Decl may be null if the base class is a template parameter.
Expand All @@ -253,9 +263,10 @@ void getInitializationsInOrder(const CXXRecordDecl &ClassDecl,
}

template <typename T>
void fixInitializerList(const ASTContext &Context, DiagnosticBuilder &Diag,
const CXXConstructorDecl *Ctor,
const SmallPtrSetImpl<const T *> &DeclsToInit) {
static void fixInitializerList(const ASTContext &Context,
DiagnosticBuilder &Diag,
const CXXConstructorDecl *Ctor,
const SmallPtrSetImpl<const T *> &DeclsToInit) {
// Do not propose fixes in macros since we cannot place them correctly.
if (Ctor->getBeginLoc().isMacroID())
return;
Expand All @@ -271,8 +282,6 @@ void fixInitializerList(const ASTContext &Context, DiagnosticBuilder &Diag,
}
}

} // anonymous namespace

ProTypeMemberInitCheck::ProTypeMemberInitCheck(StringRef Name,
ClangTidyContext *Context)
: ClangTidyCheck(Name, Context),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ AST_MATCHER_P(CoawaitExpr, awaitable, ast_matchers::internal::Matcher<Expr>,
return InnerMatcher.matches(*E, Finder, Builder);
return false;
}
} // namespace

auto typeWithNameIn(const std::vector<StringRef> &Names) {
static auto typeWithNameIn(const std::vector<StringRef> &Names) {
return hasType(
hasCanonicalType(hasDeclaration(namedDecl(hasAnyName(Names)))));
}
} // namespace

CoroutineHostileRAIICheck::CoroutineHostileRAIICheck(StringRef Name,
ClangTidyContext *Context)
Expand Down
6 changes: 3 additions & 3 deletions clang-tools-extra/clang-tidy/misc/NoRecursionCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,12 @@ constexpr unsigned SmallSCCSize = 32;
using CallStackTy =
llvm::SmallVector<CallGraphNode::CallRecord, SmallCallStackSize>;

} // namespace

// In given SCC, find *some* call stack that will be cyclic.
// This will only find *one* such stack, it might not be the smallest one,
// and there may be other loops.
CallStackTy pathfindSomeCycle(ArrayRef<CallGraphNode *> SCC) {
static CallStackTy pathfindSomeCycle(ArrayRef<CallGraphNode *> SCC) {
// We'll need to be able to performantly look up whether some CallGraphNode
// is in SCC or not, so cache all the SCC elements in a set.
const ImmutableSmallSet<CallGraphNode *, SmallSCCSize> SCCElts(SCC);
Expand Down Expand Up @@ -190,8 +192,6 @@ CallStackTy pathfindSomeCycle(ArrayRef<CallGraphNode *> SCC) {
return CallStack;
}

} // namespace

void NoRecursionCheck::registerMatchers(MatchFinder *Finder) {
Finder->addMatcher(translationUnitDecl().bind("TUDecl"), this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ struct NewSuffix {
std::optional<FixItHint> FixIt;
};

std::optional<SourceLocation> getMacroAwareLocation(SourceLocation Loc,
const SourceManager &SM) {
} // namespace

static std::optional<SourceLocation>
getMacroAwareLocation(SourceLocation Loc, const SourceManager &SM) {
// Do nothing if the provided location is invalid.
if (Loc.isInvalid())
return std::nullopt;
Expand All @@ -67,8 +69,8 @@ std::optional<SourceLocation> getMacroAwareLocation(SourceLocation Loc,
return SpellingLoc;
}

std::optional<SourceRange> getMacroAwareSourceRange(SourceRange Loc,
const SourceManager &SM) {
static std::optional<SourceRange>
getMacroAwareSourceRange(SourceRange Loc, const SourceManager &SM) {
std::optional<SourceLocation> Begin =
getMacroAwareLocation(Loc.getBegin(), SM);
std::optional<SourceLocation> End = getMacroAwareLocation(Loc.getEnd(), SM);
Expand All @@ -77,7 +79,7 @@ std::optional<SourceRange> getMacroAwareSourceRange(SourceRange Loc,
return SourceRange(*Begin, *End);
}

std::optional<std::string>
static std::optional<std::string>
getNewSuffix(llvm::StringRef OldSuffix,
const std::vector<StringRef> &NewSuffixes) {
// If there is no config, just uppercase the entirety of the suffix.
Expand All @@ -96,7 +98,7 @@ getNewSuffix(llvm::StringRef OldSuffix,
}

template <typename LiteralType>
std::optional<NewSuffix>
static std::optional<NewSuffix>
shouldReplaceLiteralSuffix(const Expr &Literal,
const std::vector<StringRef> &NewSuffixes,
const SourceManager &SM, const LangOptions &LO) {
Expand Down Expand Up @@ -174,8 +176,6 @@ shouldReplaceLiteralSuffix(const Expr &Literal,
return ReplacementDsc;
}

} // namespace

UppercaseLiteralSuffixCheck::UppercaseLiteralSuffixCheck(
StringRef Name, ClangTidyContext *Context)
: ClangTidyCheck(Name, Context),
Expand Down
22 changes: 12 additions & 10 deletions clang-tools-extra/clang-tidy/utils/DeclRefExprUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ namespace clang::tidy::utils::decl_ref_expr {
using namespace ::clang::ast_matchers;
using llvm::SmallPtrSet;

namespace {

template <typename S> bool isSetDifferenceEmpty(const S &S1, const S &S2) {
template <typename S>
static bool isSetDifferenceEmpty(const S &S1, const S &S2) {
for (auto E : S1)
if (S2.count(E) == 0)
return false;
Expand All @@ -30,15 +29,15 @@ template <typename S> bool isSetDifferenceEmpty(const S &S1, const S &S2) {

// Extracts all Nodes keyed by ID from Matches and inserts them into Nodes.
template <typename Node>
void extractNodesByIdTo(ArrayRef<BoundNodes> Matches, StringRef ID,
SmallPtrSet<const Node *, 16> &Nodes) {
static void extractNodesByIdTo(ArrayRef<BoundNodes> Matches, StringRef ID,
SmallPtrSet<const Node *, 16> &Nodes) {
for (const auto &Match : Matches)
Nodes.insert(Match.getNodeAs<Node>(ID));
}

// Returns true if both types refer to the same type,
// ignoring the const-qualifier.
bool isSameTypeIgnoringConst(QualType A, QualType B) {
static bool isSameTypeIgnoringConst(QualType A, QualType B) {
A = A.getCanonicalType();
B = B.getCanonicalType();
A.addConst();
Expand All @@ -47,7 +46,8 @@ bool isSameTypeIgnoringConst(QualType A, QualType B) {
}

// Returns true if `D` and `O` have the same parameter types.
bool hasSameParameterTypes(const CXXMethodDecl &D, const CXXMethodDecl &O) {
static bool hasSameParameterTypes(const CXXMethodDecl &D,
const CXXMethodDecl &O) {
if (D.getNumParams() != O.getNumParams())
return false;
for (int I = 0, E = D.getNumParams(); I < E; ++I) {
Expand All @@ -60,7 +60,7 @@ bool hasSameParameterTypes(const CXXMethodDecl &D, const CXXMethodDecl &O) {

// If `D` has a const-qualified overload with otherwise identical
// ref-qualifiers and parameter types, returns that overload.
const CXXMethodDecl *findConstOverload(const CXXMethodDecl &D) {
static const CXXMethodDecl *findConstOverload(const CXXMethodDecl &D) {
assert(!D.isConst());

DeclContext::lookup_result LookupResult =
Expand All @@ -81,7 +81,7 @@ const CXXMethodDecl *findConstOverload(const CXXMethodDecl &D) {

// Returns true if both types are pointers or reference to the same type,
// ignoring the const-qualifier.
bool pointsToSameTypeIgnoringConst(QualType A, QualType B) {
static bool pointsToSameTypeIgnoringConst(QualType A, QualType B) {
assert(A->isPointerType() || A->isReferenceType());
assert(B->isPointerType() || B->isReferenceType());
return isSameTypeIgnoringConst(A->getPointeeType(), B->getPointeeType());
Expand Down Expand Up @@ -122,7 +122,7 @@ bool pointsToSameTypeIgnoringConst(QualType A, QualType B) {
//
// This function checks (A) ad (B), but the caller should make sure that the
// object is not mutated through the return value.
bool isLikelyShallowConst(const CXXMethodDecl &M) {
static bool isLikelyShallowConst(const CXXMethodDecl &M) {
assert(!M.isConst());
// The method can mutate our variable.

Expand All @@ -146,6 +146,8 @@ bool isLikelyShallowConst(const CXXMethodDecl &M) {
return isSameTypeIgnoringConst(CallTy, OverloadTy);
}

namespace {

// A matcher that matches DeclRefExprs that are used in ways such that the
// underlying declaration is not modified.
// If the declaration is of pointer type, `Indirections` specifies the level
Expand Down
Loading