clang 8.0.0

PPConditionalDirectiveRecord.cpp
Go to the documentation of this file.
1 //===--- PPConditionalDirectiveRecord.h - Preprocessing Directives-*- C++ -*-=//
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 // This file implements the PPConditionalDirectiveRecord class, which maintains
11 // a record of conditional directive regions.
12 //
13 //===----------------------------------------------------------------------===//
14 #include "clang/Lex/PPConditionalDirectiveRecord.h"
15 #include "llvm/Support/Capacity.h"
16
17 using namespace clang;
18
19 PPConditionalDirectiveRecord::PPConditionalDirectiveRecord(SourceManager &SM)
20 : SourceMgr(SM) {
21 CondDirectiveStack.push_back(SourceLocation());
22 }
23
24 bool PPConditionalDirectiveRecord::rangeIntersectsConditionalDirective(
25 SourceRange Range) const {
26 if (Range.isInvalid())
27 return false;
28
29 CondDirectiveLocsTy::const_iterator
30 low = std::lower_bound(CondDirectiveLocs.begin(), CondDirectiveLocs.end(),
31 Range.getBegin(), CondDirectiveLoc::Comp(SourceMgr));
32 if (low == CondDirectiveLocs.end())
33 return false;
34
35 if (SourceMgr.isBeforeInTranslationUnit(Range.getEnd(), low->getLoc()))
36 return false;
37
38 CondDirectiveLocsTy::const_iterator
39 upp = std::upper_bound(low, CondDirectiveLocs.end(),
40 Range.getEnd(), CondDirectiveLoc::Comp(SourceMgr));
41 SourceLocation uppRegion;
42 if (upp != CondDirectiveLocs.end())
43 uppRegion = upp->getRegionLoc();
44
45 return low->getRegionLoc() != uppRegion;
46 }
47
48 SourceLocation PPConditionalDirectiveRecord::findConditionalDirectiveRegionLoc(
49 SourceLocation Loc) const {
50 if (Loc.isInvalid())
51 return SourceLocation();
52 if (CondDirectiveLocs.empty())
53 return SourceLocation();
54
55 if (SourceMgr.isBeforeInTranslationUnit(CondDirectiveLocs.back().getLoc(),
56 Loc))
57 return CondDirectiveStack.back();
58
59 CondDirectiveLocsTy::const_iterator
60 low = std::lower_bound(CondDirectiveLocs.begin(), CondDirectiveLocs.end(),
61 Loc, CondDirectiveLoc::Comp(SourceMgr));
62 assert(low != CondDirectiveLocs.end());
63 return low->getRegionLoc();
64 }
65
66 void PPConditionalDirectiveRecord::addCondDirectiveLoc(
67 CondDirectiveLoc DirLoc) {
68 // Ignore directives in system headers.
69 if (SourceMgr.isInSystemHeader(DirLoc.getLoc()))
70 return;
71
72 assert(CondDirectiveLocs.empty() ||
73 SourceMgr.isBeforeInTranslationUnit(CondDirectiveLocs.back().getLoc(),
74 DirLoc.getLoc()));
75 CondDirectiveLocs.push_back(DirLoc);
76 }
77
78 void PPConditionalDirectiveRecord::If(SourceLocation Loc,
79 SourceRange ConditionRange,
80 ConditionValueKind ConditionValue) {
81 addCondDirectiveLoc(CondDirectiveLoc(Loc, CondDirectiveStack.back()));
82 CondDirectiveStack.push_back(Loc);
83 }
84
85 void PPConditionalDirectiveRecord::Ifdef(SourceLocation Loc,
86 const Token &MacroNameTok,
87 const MacroDefinition &MD) {
88 addCondDirectiveLoc(CondDirectiveLoc(Loc, CondDirectiveStack.back()));
89 CondDirectiveStack.push_back(Loc);
90 }
91
92 void PPConditionalDirectiveRecord::Ifndef(SourceLocation Loc,
93 const Token &MacroNameTok,
94 const MacroDefinition &MD) {
95 addCondDirectiveLoc(CondDirectiveLoc(Loc, CondDirectiveStack.back()));
96 CondDirectiveStack.push_back(Loc);
97 }
98
99 void PPConditionalDirectiveRecord::Elif(SourceLocation Loc,
100 SourceRange ConditionRange,
101 ConditionValueKind ConditionValue,
102 SourceLocation IfLoc) {
103 addCondDirectiveLoc(CondDirectiveLoc(Loc, CondDirectiveStack.back()));
104 CondDirectiveStack.back() = Loc;
105 }
106
107 void PPConditionalDirectiveRecord::Else(SourceLocation Loc,
108 SourceLocation IfLoc) {
109 addCondDirectiveLoc(CondDirectiveLoc(Loc, CondDirectiveStack.back()));
110 CondDirectiveStack.back() = Loc;
111 }
112
113 void PPConditionalDirectiveRecord::Endif(SourceLocation Loc,
114 SourceLocation IfLoc) {
115 addCondDirectiveLoc(CondDirectiveLoc(Loc, CondDirectiveStack.back()));
116 assert(!CondDirectiveStack.empty());
117 CondDirectiveStack.pop_back();
118 }
119
120 size_t PPConditionalDirectiveRecord::getTotalMemory() const {
121 return llvm::capacity_in_bytes(CondDirectiveLocs);
122 }
clang::PPConditionalDirectiveRecord::CondDirectiveLoc::Comp
Definition: PPConditionalDirectiveRecord.h:42
PPConditionalDirectiveRecord.h
clang::PPConditionalDirectiveRecord::PPConditionalDirectiveRecord
PPConditionalDirectiveRecord(SourceManager &SM)
Construct a new preprocessing record.
Definition: PPConditionalDirectiveRecord.cpp:19
clang::SourceManager::isBeforeInTranslationUnit
bool isBeforeInTranslationUnit(SourceLocation LHS, SourceLocation RHS) const
Determines the order of 2 source locations in the translation unit.
Definition: SourceManager.cpp:1988
clang::MacroDefinition
A description of the current definition of a macro.
Definition: MacroInfo.h:565
clang::PPConditionalDirectiveRecord::getTotalMemory
size_t getTotalMemory() const
Definition: PPConditionalDirectiveRecord.cpp:120
clang::Token
Token - This structure provides full information about a lexed token.
Definition: Token.h:35
clang::PPCallbacks::ConditionValueKind
ConditionValueKind
Definition: PPCallbacks.h:294
clang::SourceManager::isInSystemHeader
bool isInSystemHeader(SourceLocation Loc) const
Returns if a SourceLocation is in a system header.
Definition: SourceManager.h:1445
clang::SourceRange::isInvalid
bool isInvalid() const
Definition: SourceLocation.h:214
clang::SourceRange::getEnd
SourceLocation getEnd() const
Definition: SourceLocation.h:208
SM
const SourceManager & SM
Definition: Format.cpp:1490
clang::SourceLocation
Encodes a location in the source.
Definition: SourceLocation.h:88
clang::PPConditionalDirectiveRecord::rangeIntersectsConditionalDirective
bool rangeIntersectsConditionalDirective(SourceRange Range) const
Returns true if the given range intersects with a conditional directive.
Definition: PPConditionalDirectiveRecord.cpp:24
clang
Dataflow Directional Tag Classes.
Definition: CFGReachabilityAnalysis.h:22
clang::PPConditionalDirectiveRecord::findConditionalDirectiveRegionLoc
SourceLocation findConditionalDirectiveRegionLoc(SourceLocation Loc) const
Definition: PPConditionalDirectiveRecord.cpp:48
clang::SourceLocation::isInvalid
bool isInvalid() const
Definition: SourceLocation.h:109
clang::SourceRange
A trivial tuple used to represent a source range.
Definition: SourceLocation.h:198
clang::SourceRange::getBegin
SourceLocation getBegin() const
Definition: SourceLocation.h:207
clang::SourceManager
This class handles loading and caching of source files into memory.
Definition: SourceManager.h:582