annotate clang/lib/Lex/PPConditionalDirectiveRecord.cpp @ 176:de4ac79aef9d

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 25 May 2020 17:13:11 +0900
parents 1d019706d866
children 2e18cbf3894f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 //===--- PPConditionalDirectiveRecord.h - Preprocessing Directives-*- C++ -*-=//
anatofuz
parents:
diff changeset
2 //
anatofuz
parents:
diff changeset
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
anatofuz
parents:
diff changeset
4 // See https://llvm.org/LICENSE.txt for license information.
anatofuz
parents:
diff changeset
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
anatofuz
parents:
diff changeset
6 //
anatofuz
parents:
diff changeset
7 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
8 //
anatofuz
parents:
diff changeset
9 // This file implements the PPConditionalDirectiveRecord class, which maintains
anatofuz
parents:
diff changeset
10 // a record of conditional directive regions.
anatofuz
parents:
diff changeset
11 //
anatofuz
parents:
diff changeset
12 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
13 #include "clang/Lex/PPConditionalDirectiveRecord.h"
anatofuz
parents:
diff changeset
14 #include "llvm/Support/Capacity.h"
anatofuz
parents:
diff changeset
15
anatofuz
parents:
diff changeset
16 using namespace clang;
anatofuz
parents:
diff changeset
17
anatofuz
parents:
diff changeset
18 PPConditionalDirectiveRecord::PPConditionalDirectiveRecord(SourceManager &SM)
anatofuz
parents:
diff changeset
19 : SourceMgr(SM) {
anatofuz
parents:
diff changeset
20 CondDirectiveStack.push_back(SourceLocation());
anatofuz
parents:
diff changeset
21 }
anatofuz
parents:
diff changeset
22
anatofuz
parents:
diff changeset
23 bool PPConditionalDirectiveRecord::rangeIntersectsConditionalDirective(
anatofuz
parents:
diff changeset
24 SourceRange Range) const {
anatofuz
parents:
diff changeset
25 if (Range.isInvalid())
anatofuz
parents:
diff changeset
26 return false;
anatofuz
parents:
diff changeset
27
anatofuz
parents:
diff changeset
28 CondDirectiveLocsTy::const_iterator low = llvm::lower_bound(
anatofuz
parents:
diff changeset
29 CondDirectiveLocs, Range.getBegin(), CondDirectiveLoc::Comp(SourceMgr));
anatofuz
parents:
diff changeset
30 if (low == CondDirectiveLocs.end())
anatofuz
parents:
diff changeset
31 return false;
anatofuz
parents:
diff changeset
32
anatofuz
parents:
diff changeset
33 if (SourceMgr.isBeforeInTranslationUnit(Range.getEnd(), low->getLoc()))
anatofuz
parents:
diff changeset
34 return false;
anatofuz
parents:
diff changeset
35
anatofuz
parents:
diff changeset
36 CondDirectiveLocsTy::const_iterator
anatofuz
parents:
diff changeset
37 upp = std::upper_bound(low, CondDirectiveLocs.end(),
anatofuz
parents:
diff changeset
38 Range.getEnd(), CondDirectiveLoc::Comp(SourceMgr));
anatofuz
parents:
diff changeset
39 SourceLocation uppRegion;
anatofuz
parents:
diff changeset
40 if (upp != CondDirectiveLocs.end())
anatofuz
parents:
diff changeset
41 uppRegion = upp->getRegionLoc();
anatofuz
parents:
diff changeset
42
anatofuz
parents:
diff changeset
43 return low->getRegionLoc() != uppRegion;
anatofuz
parents:
diff changeset
44 }
anatofuz
parents:
diff changeset
45
anatofuz
parents:
diff changeset
46 SourceLocation PPConditionalDirectiveRecord::findConditionalDirectiveRegionLoc(
anatofuz
parents:
diff changeset
47 SourceLocation Loc) const {
anatofuz
parents:
diff changeset
48 if (Loc.isInvalid())
anatofuz
parents:
diff changeset
49 return SourceLocation();
anatofuz
parents:
diff changeset
50 if (CondDirectiveLocs.empty())
anatofuz
parents:
diff changeset
51 return SourceLocation();
anatofuz
parents:
diff changeset
52
anatofuz
parents:
diff changeset
53 if (SourceMgr.isBeforeInTranslationUnit(CondDirectiveLocs.back().getLoc(),
anatofuz
parents:
diff changeset
54 Loc))
anatofuz
parents:
diff changeset
55 return CondDirectiveStack.back();
anatofuz
parents:
diff changeset
56
anatofuz
parents:
diff changeset
57 CondDirectiveLocsTy::const_iterator low = llvm::lower_bound(
anatofuz
parents:
diff changeset
58 CondDirectiveLocs, Loc, CondDirectiveLoc::Comp(SourceMgr));
anatofuz
parents:
diff changeset
59 assert(low != CondDirectiveLocs.end());
anatofuz
parents:
diff changeset
60 return low->getRegionLoc();
anatofuz
parents:
diff changeset
61 }
anatofuz
parents:
diff changeset
62
anatofuz
parents:
diff changeset
63 void PPConditionalDirectiveRecord::addCondDirectiveLoc(
anatofuz
parents:
diff changeset
64 CondDirectiveLoc DirLoc) {
anatofuz
parents:
diff changeset
65 // Ignore directives in system headers.
anatofuz
parents:
diff changeset
66 if (SourceMgr.isInSystemHeader(DirLoc.getLoc()))
anatofuz
parents:
diff changeset
67 return;
anatofuz
parents:
diff changeset
68
anatofuz
parents:
diff changeset
69 assert(CondDirectiveLocs.empty() ||
anatofuz
parents:
diff changeset
70 SourceMgr.isBeforeInTranslationUnit(CondDirectiveLocs.back().getLoc(),
anatofuz
parents:
diff changeset
71 DirLoc.getLoc()));
anatofuz
parents:
diff changeset
72 CondDirectiveLocs.push_back(DirLoc);
anatofuz
parents:
diff changeset
73 }
anatofuz
parents:
diff changeset
74
anatofuz
parents:
diff changeset
75 void PPConditionalDirectiveRecord::If(SourceLocation Loc,
anatofuz
parents:
diff changeset
76 SourceRange ConditionRange,
anatofuz
parents:
diff changeset
77 ConditionValueKind ConditionValue) {
anatofuz
parents:
diff changeset
78 addCondDirectiveLoc(CondDirectiveLoc(Loc, CondDirectiveStack.back()));
anatofuz
parents:
diff changeset
79 CondDirectiveStack.push_back(Loc);
anatofuz
parents:
diff changeset
80 }
anatofuz
parents:
diff changeset
81
anatofuz
parents:
diff changeset
82 void PPConditionalDirectiveRecord::Ifdef(SourceLocation Loc,
anatofuz
parents:
diff changeset
83 const Token &MacroNameTok,
anatofuz
parents:
diff changeset
84 const MacroDefinition &MD) {
anatofuz
parents:
diff changeset
85 addCondDirectiveLoc(CondDirectiveLoc(Loc, CondDirectiveStack.back()));
anatofuz
parents:
diff changeset
86 CondDirectiveStack.push_back(Loc);
anatofuz
parents:
diff changeset
87 }
anatofuz
parents:
diff changeset
88
anatofuz
parents:
diff changeset
89 void PPConditionalDirectiveRecord::Ifndef(SourceLocation Loc,
anatofuz
parents:
diff changeset
90 const Token &MacroNameTok,
anatofuz
parents:
diff changeset
91 const MacroDefinition &MD) {
anatofuz
parents:
diff changeset
92 addCondDirectiveLoc(CondDirectiveLoc(Loc, CondDirectiveStack.back()));
anatofuz
parents:
diff changeset
93 CondDirectiveStack.push_back(Loc);
anatofuz
parents:
diff changeset
94 }
anatofuz
parents:
diff changeset
95
anatofuz
parents:
diff changeset
96 void PPConditionalDirectiveRecord::Elif(SourceLocation Loc,
anatofuz
parents:
diff changeset
97 SourceRange ConditionRange,
anatofuz
parents:
diff changeset
98 ConditionValueKind ConditionValue,
anatofuz
parents:
diff changeset
99 SourceLocation IfLoc) {
anatofuz
parents:
diff changeset
100 addCondDirectiveLoc(CondDirectiveLoc(Loc, CondDirectiveStack.back()));
anatofuz
parents:
diff changeset
101 CondDirectiveStack.back() = Loc;
anatofuz
parents:
diff changeset
102 }
anatofuz
parents:
diff changeset
103
anatofuz
parents:
diff changeset
104 void PPConditionalDirectiveRecord::Else(SourceLocation Loc,
anatofuz
parents:
diff changeset
105 SourceLocation IfLoc) {
anatofuz
parents:
diff changeset
106 addCondDirectiveLoc(CondDirectiveLoc(Loc, CondDirectiveStack.back()));
anatofuz
parents:
diff changeset
107 CondDirectiveStack.back() = Loc;
anatofuz
parents:
diff changeset
108 }
anatofuz
parents:
diff changeset
109
anatofuz
parents:
diff changeset
110 void PPConditionalDirectiveRecord::Endif(SourceLocation Loc,
anatofuz
parents:
diff changeset
111 SourceLocation IfLoc) {
anatofuz
parents:
diff changeset
112 addCondDirectiveLoc(CondDirectiveLoc(Loc, CondDirectiveStack.back()));
anatofuz
parents:
diff changeset
113 assert(!CondDirectiveStack.empty());
anatofuz
parents:
diff changeset
114 CondDirectiveStack.pop_back();
anatofuz
parents:
diff changeset
115 }
anatofuz
parents:
diff changeset
116
anatofuz
parents:
diff changeset
117 size_t PPConditionalDirectiveRecord::getTotalMemory() const {
anatofuz
parents:
diff changeset
118 return llvm::capacity_in_bytes(CondDirectiveLocs);
anatofuz
parents:
diff changeset
119 }