clang  14.0.0git
PPConditionalDirectiveRecord.h
Go to the documentation of this file.
1 //===--- PPConditionalDirectiveRecord.h - Preprocessing Directives-*- C++ -*-=//
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 // This file defines the PPConditionalDirectiveRecord class, which maintains
10 // a record of conditional directive regions.
11 //
12 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_CLANG_LEX_PPCONDITIONALDIRECTIVERECORD_H
14 #define LLVM_CLANG_LEX_PPCONDITIONALDIRECTIVERECORD_H
15 
17 #include "clang/Lex/PPCallbacks.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include <vector>
20 
21 namespace clang {
22 
23 /// Records preprocessor conditional directive regions and allows
24 /// querying in which region source locations belong to.
26  SourceManager &SourceMgr;
27 
28  SmallVector<SourceLocation, 6> CondDirectiveStack;
29 
30  class CondDirectiveLoc {
31  SourceLocation Loc;
32  SourceLocation RegionLoc;
33 
34  public:
35  CondDirectiveLoc(SourceLocation Loc, SourceLocation RegionLoc)
36  : Loc(Loc), RegionLoc(RegionLoc) {}
37 
38  SourceLocation getLoc() const { return Loc; }
39  SourceLocation getRegionLoc() const { return RegionLoc; }
40 
41  class Comp {
42  SourceManager &SM;
43  public:
44  explicit Comp(SourceManager &SM) : SM(SM) {}
45  bool operator()(const CondDirectiveLoc &LHS,
46  const CondDirectiveLoc &RHS) {
47  return SM.isBeforeInTranslationUnit(LHS.getLoc(), RHS.getLoc());
48  }
49  bool operator()(const CondDirectiveLoc &LHS, SourceLocation RHS) {
50  return SM.isBeforeInTranslationUnit(LHS.getLoc(), RHS);
51  }
52  bool operator()(SourceLocation LHS, const CondDirectiveLoc &RHS) {
53  return SM.isBeforeInTranslationUnit(LHS, RHS.getLoc());
54  }
55  };
56  };
57 
58  typedef std::vector<CondDirectiveLoc> CondDirectiveLocsTy;
59  /// The locations of conditional directives in source order.
60  CondDirectiveLocsTy CondDirectiveLocs;
61 
62  void addCondDirectiveLoc(CondDirectiveLoc DirLoc);
63 
64 public:
65  /// Construct a new preprocessing record.
67 
68  size_t getTotalMemory() const;
69 
70  SourceManager &getSourceManager() const { return SourceMgr; }
71 
72  /// Returns true if the given range intersects with a conditional
73  /// directive. if a \#if/\#endif block is fully contained within the range,
74  /// this function will return false.
76 
77  /// Returns true if the given locations are in different regions,
78  /// separated by conditional directive blocks.
80  SourceLocation RHS) const {
83  }
84 
86 
87 private:
88  void If(SourceLocation Loc, SourceRange ConditionRange,
89  ConditionValueKind ConditionValue) override;
90  void Elif(SourceLocation Loc, SourceRange ConditionRange,
91  ConditionValueKind ConditionValue, SourceLocation IfLoc) override;
92  void Ifdef(SourceLocation Loc, const Token &MacroNameTok,
93  const MacroDefinition &MD) override;
94  void Ifndef(SourceLocation Loc, const Token &MacroNameTok,
95  const MacroDefinition &MD) override;
96  void Elifdef(SourceLocation Loc, const Token &MacroNameTok,
97  const MacroDefinition &MD) override;
98  void Elifdef(SourceLocation Loc, SourceRange ConditionRange,
99  SourceLocation IfLoc) override;
100  void Elifndef(SourceLocation Loc, const Token &MacroNameTok,
101  const MacroDefinition &MD) override;
102  void Elifndef(SourceLocation Loc, SourceRange ConditionRange,
103  SourceLocation IfLoc) override;
104  void Else(SourceLocation Loc, SourceLocation IfLoc) override;
105  void Endif(SourceLocation Loc, SourceLocation IfLoc) override;
106 };
107 
108 } // end namespace clang
109 
110 #endif // LLVM_CLANG_LEX_PPCONDITIONALDIRECTIVERECORD_H
clang::PPConditionalDirectiveRecord::rangeIntersectsConditionalDirective
bool rangeIntersectsConditionalDirective(SourceRange Range) const
Returns true if the given range intersects with a conditional directive.
Definition: PPConditionalDirectiveRecord.cpp:23
clang::PPConditionalDirectiveRecord::CondDirectiveLoc::Comp
Definition: PPConditionalDirectiveRecord.h:41
clang::PPConditionalDirectiveRecord::CondDirectiveLoc::Comp::Comp
Comp(SourceManager &SM)
Definition: PPConditionalDirectiveRecord.h:44
clang::SourceRange
A trivial tuple used to represent a source range.
Definition: SourceLocation.h:212
clang::MacroDefinition
A description of the current definition of a macro.
Definition: MacroInfo.h:564
llvm::SmallVector
Definition: LLVM.h:38
clang::SourceLocation
Encodes a location in the source.
Definition: SourceLocation.h:88
clang::PPCallbacks
This interface provides a way to observe the actions of the preprocessor as it does its thing.
Definition: PPCallbacks.h:35
clang::PPConditionalDirectiveRecord::CondDirectiveLoc::Comp::operator()
bool operator()(const CondDirectiveLoc &LHS, const CondDirectiveLoc &RHS)
Definition: PPConditionalDirectiveRecord.h:45
clang::Token
Token - This structure provides full information about a lexed token.
Definition: Token.h:34
clang::SourceManager
This class handles loading and caching of source files into memory.
Definition: SourceManager.h:626
clang::PPConditionalDirectiveRecord::CondDirectiveLoc::Comp::operator()
bool operator()(const CondDirectiveLoc &LHS, SourceLocation RHS)
Definition: PPConditionalDirectiveRecord.h:49
clang::PPCallbacks::ConditionValueKind
ConditionValueKind
Definition: PPCallbacks.h:337
clang::PPConditionalDirectiveRecord::PPConditionalDirectiveRecord
PPConditionalDirectiveRecord(SourceManager &SM)
Construct a new preprocessing record.
Definition: PPConditionalDirectiveRecord.cpp:18
clang::PPConditionalDirectiveRecord::findConditionalDirectiveRegionLoc
SourceLocation findConditionalDirectiveRegionLoc(SourceLocation Loc) const
Definition: PPConditionalDirectiveRecord.cpp:46
clang::PPConditionalDirectiveRecord::areInDifferentConditionalDirectiveRegion
bool areInDifferentConditionalDirectiveRegion(SourceLocation LHS, SourceLocation RHS) const
Returns true if the given locations are in different regions, separated by conditional directive bloc...
Definition: PPConditionalDirectiveRecord.h:79
SourceLocation.h
clang::PPConditionalDirectiveRecord
Records preprocessor conditional directive regions and allows querying in which region source locatio...
Definition: PPConditionalDirectiveRecord.h:25
clang::PPConditionalDirectiveRecord::getTotalMemory
size_t getTotalMemory() const
Definition: PPConditionalDirectiveRecord.cpp:139
clang::PPConditionalDirectiveRecord::CondDirectiveLoc::Comp::operator()
bool operator()(SourceLocation LHS, const CondDirectiveLoc &RHS)
Definition: PPConditionalDirectiveRecord.h:52
clang
Definition: CalledOnceCheck.h:17
PPCallbacks.h
clang::PPConditionalDirectiveRecord::getSourceManager
SourceManager & getSourceManager() const
Definition: PPConditionalDirectiveRecord.h:70
SM
#define SM(sm)
Definition: Cuda.cpp:78