clang-tools 6.0.0

ASTUtils.cpp
Go to the documentation of this file.
1 //===---------- ASTUtils.cpp - clang-tidy ---------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "ASTUtils.h"
11
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 #include "clang/ASTMatchers/ASTMatchers.h"
14 #include "clang/Lex/Lexer.h"
15
16 namespace clang {
17 namespace tidy {
18 namespace utils {
19 using namespace ast_matchers;
20
21 const FunctionDecl *getSurroundingFunction(ASTContext &Context,
22 const Stmt &Statement) {
23 return selectFirst<const FunctionDecl>(
24 "function", match(stmt(hasAncestor(functionDecl().bind("function"))),
25 Statement, Context));
26 }
27
28 bool IsBinaryOrTernary(const Expr *E) {
29 const Expr *E_base = E->IgnoreImpCasts();
30 if (clang::isa<clang::BinaryOperator>(E_base) ||
31 clang::isa<clang::ConditionalOperator>(E_base)) {
32 return true;
33 }
34
35 if (const auto *Operator =
36 clang::dyn_cast<clang::CXXOperatorCallExpr>(E_base)) {
37 return Operator->isInfixBinaryOp();
38 }
39
40 return false;
41 }
42
43 bool exprHasBitFlagWithSpelling(const Expr *Flags, const SourceManager &SM,
44 const LangOptions &LangOpts,
45 StringRef FlagName) {
46 // If the Flag is an integer constant, check it.
47 if (isa<IntegerLiteral>(Flags)) {
48 if (!SM.isMacroBodyExpansion(Flags->getLocStart()) &&
49 !SM.isMacroArgExpansion(Flags->getLocStart()))
50 return false;
51
52 // Get the marco name.
53 auto MacroName = Lexer::getSourceText(
54 CharSourceRange::getTokenRange(Flags->getSourceRange()), SM, LangOpts);
55
56 return MacroName == FlagName;
57 }
58 // If it's a binary OR operation.
59 if (const auto *BO = dyn_cast<BinaryOperator>(Flags))
60 if (BO->getOpcode() == clang::BinaryOperatorKind::BO_Or)
61 return exprHasBitFlagWithSpelling(BO->getLHS()->IgnoreParenCasts(), SM,
62 LangOpts, FlagName) ||
63 exprHasBitFlagWithSpelling(BO->getRHS()->IgnoreParenCasts(), SM,
64 LangOpts, FlagName);
65
66 // Otherwise, assume it has the flag.
67 return true;
68 }
69
70 } // namespace utils
71 } // namespace tidy
72 } // namespace clang
clang::tidy::utils::IsBinaryOrTernary
bool IsBinaryOrTernary(const Expr *E)
Definition: ASTUtils.cpp:28
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:43
clang::tidy::utils::getSurroundingFunction
const FunctionDecl * getSurroundingFunction(ASTContext &Context, const Stmt &Statement)
Definition: ASTUtils.cpp:21
clang
Definition: AndroidTidyModule.cpp:28
ASTUtils.h