clang-tools 22.0.0git
ASTUtils.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "ASTUtils.h"
10
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12#include "clang/ASTMatchers/ASTMatchers.h"
13#include "clang/Lex/Lexer.h"
14
15namespace clang::tidy::utils {
16using namespace ast_matchers;
17
18const FunctionDecl *getSurroundingFunction(ASTContext &Context,
19 const Stmt &Statement) {
20 return selectFirst<const FunctionDecl>(
21 "function", match(stmt(hasAncestor(functionDecl().bind("function"))),
22 Statement, Context));
23}
24
25bool isBinaryOrTernary(const Expr *E) {
26 const Expr *EBase = E->IgnoreImpCasts();
27 if (isa<BinaryOperator>(EBase) || isa<ConditionalOperator>(EBase))
28 return true;
29
30 if (const auto *Operator = dyn_cast<CXXOperatorCallExpr>(EBase))
31 return Operator->isInfixBinaryOp();
32
33 return false;
34}
35
36bool exprHasBitFlagWithSpelling(const Expr *Flags, const SourceManager &SM,
37 const LangOptions &LangOpts,
38 StringRef FlagName) {
39 // If the Flag is an integer constant, check it.
40 if (isa<IntegerLiteral>(Flags)) {
41 if (!SM.isMacroBodyExpansion(Flags->getBeginLoc()) &&
42 !SM.isMacroArgExpansion(Flags->getBeginLoc()))
43 return false;
44
45 // Get the macro name.
46 auto MacroName = Lexer::getSourceText(
47 CharSourceRange::getTokenRange(Flags->getSourceRange()), SM, LangOpts);
48
49 return MacroName == FlagName;
50 }
51 // If it's a binary OR operation.
52 if (const auto *BO = dyn_cast<BinaryOperator>(Flags))
53 if (BO->getOpcode() == BinaryOperatorKind::BO_Or)
54 return exprHasBitFlagWithSpelling(BO->getLHS()->IgnoreParenCasts(), SM,
55 LangOpts, FlagName) ||
56 exprHasBitFlagWithSpelling(BO->getRHS()->IgnoreParenCasts(), SM,
57 LangOpts, FlagName);
58
59 // Otherwise, assume it has the flag.
60 return true;
61}
62
64 const SourceManager *SM) {
65 // Check if the range is entirely contained within a macro argument.
66 SourceLocation MacroArgExpansionStartForRangeBegin;
67 SourceLocation MacroArgExpansionStartForRangeEnd;
68 const bool RangeIsEntirelyWithinMacroArgument =
69 SM &&
70 SM->isMacroArgExpansion(Range.getBegin(),
71 &MacroArgExpansionStartForRangeBegin) &&
72 SM->isMacroArgExpansion(Range.getEnd(),
73 &MacroArgExpansionStartForRangeEnd) &&
74 MacroArgExpansionStartForRangeBegin == MacroArgExpansionStartForRangeEnd;
75
76 return RangeIsEntirelyWithinMacroArgument;
77}
78
79bool rangeContainsMacroExpansion(SourceRange Range, const SourceManager *SM) {
80 return rangeIsEntirelyWithinMacroArgument(Range, SM) ||
81 Range.getBegin().isMacroID() || Range.getEnd().isMacroID();
82}
83
84bool rangeCanBeFixed(SourceRange Range, const SourceManager *SM) {
87}
88
89bool areStatementsIdentical(const Stmt *FirstStmt, const Stmt *SecondStmt,
90 const ASTContext &Context, bool Canonical) {
91 if (!FirstStmt || !SecondStmt)
92 return false;
93
94 if (FirstStmt == SecondStmt)
95 return true;
96
97 if (FirstStmt->getStmtClass() != SecondStmt->getStmtClass())
98 return false;
99
100 if (isa<Expr>(FirstStmt) && isa<Expr>(SecondStmt)) {
101 // If we have errors in expressions, we will be unable
102 // to accurately profile and compute hashes for each statements.
103 if (llvm::cast<Expr>(FirstStmt)->containsErrors() ||
104 llvm::cast<Expr>(SecondStmt)->containsErrors())
105 return false;
106 }
107
108 llvm::FoldingSetNodeID DataFirst, DataSecond;
109 FirstStmt->Profile(DataFirst, Context, Canonical);
110 SecondStmt->Profile(DataSecond, Context, Canonical);
111 return DataFirst == DataSecond;
112}
113
114const IndirectFieldDecl *
116 const RecordDecl *Record = FD->getParent();
117 assert(Record->isAnonymousStructOrUnion() &&
118 "FD must be a field in an anonymous record");
119
120 const DeclContext *Context = Record;
121 while (isa<RecordDecl>(Context) &&
122 cast<RecordDecl>(Context)->isAnonymousStructOrUnion()) {
123 Context = Context->getParent();
124 }
125
126 // Search for the target IndirectFieldDecl within the located context.
127 for (const auto *D : Context->decls()) {
128 const auto *IFD = dyn_cast<IndirectFieldDecl>(D);
129 if (!IFD)
130 continue;
131 if (IFD->getAnonField() == FD)
132 return IFD;
133 }
134
135 return nullptr;
136}
137
138} // namespace clang::tidy::utils
bool rangeCanBeFixed(SourceRange Range, const SourceManager *SM)
Definition ASTUtils.cpp:84
bool isBinaryOrTernary(const Expr *E)
Definition ASTUtils.cpp:25
bool rangeContainsMacroExpansion(SourceRange Range, const SourceManager *SM)
Definition ASTUtils.cpp:79
bool areStatementsIdentical(const Stmt *FirstStmt, const Stmt *SecondStmt, const ASTContext &Context, bool Canonical)
Definition ASTUtils.cpp:89
bool exprHasBitFlagWithSpelling(const Expr *Flags, const SourceManager &SM, const LangOptions &LangOpts, StringRef FlagName)
Checks whether a macro flag is present in the given argument.
Definition ASTUtils.cpp:36
const IndirectFieldDecl * findOutermostIndirectFieldDeclForField(const FieldDecl *FD)
Definition ASTUtils.cpp:115
bool rangeIsEntirelyWithinMacroArgument(SourceRange Range, const SourceManager *SM)
Definition ASTUtils.cpp:63
const FunctionDecl * getSurroundingFunction(ASTContext &Context, const Stmt &Statement)
Definition ASTUtils.cpp:18