clang-tools  8.0.0svn
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->getBeginLoc()) &&
49  !SM.isMacroArgExpansion(Flags->getBeginLoc()))
50  return false;
51 
52  // Get the macro 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 
71  const SourceManager *SM) {
72  // Check if the range is entirely contained within a macro argument.
73  SourceLocation MacroArgExpansionStartForRangeBegin;
74  SourceLocation MacroArgExpansionStartForRangeEnd;
75  bool RangeIsEntirelyWithinMacroArgument =
76  SM &&
77  SM->isMacroArgExpansion(Range.getBegin(),
78  &MacroArgExpansionStartForRangeBegin) &&
79  SM->isMacroArgExpansion(Range.getEnd(),
80  &MacroArgExpansionStartForRangeEnd) &&
81  MacroArgExpansionStartForRangeBegin == MacroArgExpansionStartForRangeEnd;
82 
83  return RangeIsEntirelyWithinMacroArgument;
84 }
85 
86 bool rangeContainsMacroExpansion(SourceRange Range, const SourceManager *SM) {
87  return rangeIsEntirelyWithinMacroArgument(Range, SM) ||
88  Range.getBegin().isMacroID() || Range.getEnd().isMacroID();
89 }
90 
91 bool rangeCanBeFixed(SourceRange Range, const SourceManager *SM) {
94 }
95 
96 } // namespace utils
97 } // namespace tidy
98 } // namespace clang
bool IsBinaryOrTernary(const Expr *E)
Definition: ASTUtils.cpp:28
bool rangeIsEntirelyWithinMacroArgument(SourceRange Range, const SourceManager *SM)
Definition: ASTUtils.cpp:70
bool rangeContainsMacroExpansion(SourceRange Range, const SourceManager *SM)
Definition: ASTUtils.cpp:86
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
const FunctionDecl * getSurroundingFunction(ASTContext &Context, const Stmt &Statement)
Definition: ASTUtils.cpp:21
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
CharSourceRange Range
SourceRange for the file name.
bool rangeCanBeFixed(SourceRange Range, const SourceManager *SM)
Definition: ASTUtils.cpp:91