clang-tools  14.0.0git
ASTUtils.cpp
Go to the documentation of this file.
1 //===---------- ASTUtils.cpp - clang-tidy ---------------------------------===//
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 
15 namespace clang {
16 namespace tidy {
17 namespace utils {
18 using namespace ast_matchers;
19 
20 const FunctionDecl *getSurroundingFunction(ASTContext &Context,
21  const Stmt &Statement) {
22  return selectFirst<const FunctionDecl>(
23  "function", match(stmt(hasAncestor(functionDecl().bind("function"))),
24  Statement, Context));
25 }
26 
27 bool IsBinaryOrTernary(const Expr *E) {
28  const Expr *EBase = E->IgnoreImpCasts();
29  if (isa<BinaryOperator>(EBase) || isa<ConditionalOperator>(EBase)) {
30  return true;
31  }
32 
33  if (const auto *Operator = dyn_cast<CXXOperatorCallExpr>(EBase)) {
34  return Operator->isInfixBinaryOp();
35  }
36 
37  return false;
38 }
39 
40 bool exprHasBitFlagWithSpelling(const Expr *Flags, const SourceManager &SM,
41  const LangOptions &LangOpts,
42  StringRef FlagName) {
43  // If the Flag is an integer constant, check it.
44  if (isa<IntegerLiteral>(Flags)) {
45  if (!SM.isMacroBodyExpansion(Flags->getBeginLoc()) &&
46  !SM.isMacroArgExpansion(Flags->getBeginLoc()))
47  return false;
48 
49  // Get the macro name.
50  auto MacroName = Lexer::getSourceText(
51  CharSourceRange::getTokenRange(Flags->getSourceRange()), SM, LangOpts);
52 
53  return MacroName == FlagName;
54  }
55  // If it's a binary OR operation.
56  if (const auto *BO = dyn_cast<BinaryOperator>(Flags))
57  if (BO->getOpcode() == BinaryOperatorKind::BO_Or)
58  return exprHasBitFlagWithSpelling(BO->getLHS()->IgnoreParenCasts(), SM,
59  LangOpts, FlagName) ||
60  exprHasBitFlagWithSpelling(BO->getRHS()->IgnoreParenCasts(), SM,
61  LangOpts, FlagName);
62 
63  // Otherwise, assume it has the flag.
64  return true;
65 }
66 
68  const SourceManager *SM) {
69  // Check if the range is entirely contained within a macro argument.
70  SourceLocation MacroArgExpansionStartForRangeBegin;
71  SourceLocation MacroArgExpansionStartForRangeEnd;
72  bool RangeIsEntirelyWithinMacroArgument =
73  SM &&
74  SM->isMacroArgExpansion(Range.getBegin(),
75  &MacroArgExpansionStartForRangeBegin) &&
76  SM->isMacroArgExpansion(Range.getEnd(),
77  &MacroArgExpansionStartForRangeEnd) &&
78  MacroArgExpansionStartForRangeBegin == MacroArgExpansionStartForRangeEnd;
79 
80  return RangeIsEntirelyWithinMacroArgument;
81 }
82 
83 bool rangeContainsMacroExpansion(SourceRange Range, const SourceManager *SM) {
85  Range.getBegin().isMacroID() || Range.getEnd().isMacroID();
86 }
87 
88 bool rangeCanBeFixed(SourceRange Range, const SourceManager *SM) {
91 }
92 
93 } // namespace utils
94 } // namespace tidy
95 } // namespace clang
Range
CharSourceRange Range
SourceRange for the file name.
Definition: IncludeOrderCheck.cpp:38
Flags
MixFlags Flags
Definition: EasilySwappableParametersCheck.cpp:1086
E
const Expr * E
Definition: AvoidBindCheck.cpp:88
clang::tidy::utils::getSurroundingFunction
const FunctionDecl * getSurroundingFunction(ASTContext &Context, const Stmt &Statement)
Definition: ASTUtils.cpp:20
clang::tidy::cppcoreguidelines::getSourceText
static std::string getSourceText(const CXXDestructorDecl &Destructor)
Definition: VirtualClassDestructorCheck.cpp:109
clang::clangd::match
std::vector< std::string > match(const SymbolIndex &I, const FuzzyFindRequest &Req, bool *Incomplete)
Definition: TestIndex.cpp:94
clang::tidy::utils::exprHasBitFlagWithSpelling
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:40
clang::tidy::utils::IsBinaryOrTernary
bool IsBinaryOrTernary(const Expr *E)
Definition: ASTUtils.cpp:27
ASTUtils.h
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::tidy::utils::rangeCanBeFixed
bool rangeCanBeFixed(SourceRange Range, const SourceManager *SM)
Definition: ASTUtils.cpp:88
SM
const SourceManager & SM
Definition: IncludeCleaner.cpp:108
clang::tidy::utils::rangeIsEntirelyWithinMacroArgument
bool rangeIsEntirelyWithinMacroArgument(SourceRange Range, const SourceManager *SM)
Definition: ASTUtils.cpp:67
clang::tidy::utils::rangeContainsMacroExpansion
bool rangeContainsMacroExpansion(SourceRange Range, const SourceManager *SM)
Definition: ASTUtils.cpp:83