/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Lex/PPConditionalDirectiveRecord.cpp
Line | Count | Source (jump to first uncovered line) |
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 implements the PPConditionalDirectiveRecord class, which maintains |
10 | | // a record of conditional directive regions. |
11 | | // |
12 | | //===----------------------------------------------------------------------===// |
13 | | #include "clang/Lex/PPConditionalDirectiveRecord.h" |
14 | | #include "llvm/Support/Capacity.h" |
15 | | |
16 | | using namespace clang; |
17 | | |
18 | | PPConditionalDirectiveRecord::PPConditionalDirectiveRecord(SourceManager &SM) |
19 | 50 | : SourceMgr(SM) { |
20 | 50 | CondDirectiveStack.push_back(SourceLocation()); |
21 | 50 | } |
22 | | |
23 | | bool PPConditionalDirectiveRecord::rangeIntersectsConditionalDirective( |
24 | 1.22k | SourceRange Range) const { |
25 | 1.22k | if (Range.isInvalid()) |
26 | 0 | return false; |
27 | | |
28 | 1.22k | CondDirectiveLocsTy::const_iterator low = llvm::lower_bound( |
29 | 1.22k | CondDirectiveLocs, Range.getBegin(), CondDirectiveLoc::Comp(SourceMgr)); |
30 | 1.22k | if (low == CondDirectiveLocs.end()) |
31 | 1.11k | return false; |
32 | | |
33 | 112 | if (SourceMgr.isBeforeInTranslationUnit(Range.getEnd(), low->getLoc())) |
34 | 94 | return false; |
35 | | |
36 | 18 | CondDirectiveLocsTy::const_iterator |
37 | 18 | upp = std::upper_bound(low, CondDirectiveLocs.end(), |
38 | 18 | Range.getEnd(), CondDirectiveLoc::Comp(SourceMgr)); |
39 | 18 | SourceLocation uppRegion; |
40 | 18 | if (upp != CondDirectiveLocs.end()) |
41 | 14 | uppRegion = upp->getRegionLoc(); |
42 | | |
43 | 18 | return low->getRegionLoc() != uppRegion; |
44 | 112 | } |
45 | | |
46 | | SourceLocation PPConditionalDirectiveRecord::findConditionalDirectiveRegionLoc( |
47 | 131 | SourceLocation Loc) const { |
48 | 131 | if (Loc.isInvalid()) |
49 | 0 | return SourceLocation(); |
50 | 131 | if (CondDirectiveLocs.empty()) |
51 | 58 | return SourceLocation(); |
52 | | |
53 | 73 | if (SourceMgr.isBeforeInTranslationUnit(CondDirectiveLocs.back().getLoc(), |
54 | 73 | Loc)) |
55 | 39 | return CondDirectiveStack.back(); |
56 | | |
57 | 34 | CondDirectiveLocsTy::const_iterator low = llvm::lower_bound( |
58 | 34 | CondDirectiveLocs, Loc, CondDirectiveLoc::Comp(SourceMgr)); |
59 | 34 | assert(low != CondDirectiveLocs.end()); |
60 | 34 | return low->getRegionLoc(); |
61 | 34 | } |
62 | | |
63 | | void PPConditionalDirectiveRecord::addCondDirectiveLoc( |
64 | 170 | CondDirectiveLoc DirLoc) { |
65 | | // Ignore directives in system headers. |
66 | 170 | if (SourceMgr.isInSystemHeader(DirLoc.getLoc())) |
67 | 69 | return; |
68 | | |
69 | 101 | assert(CondDirectiveLocs.empty() || |
70 | 101 | SourceMgr.isBeforeInTranslationUnit(CondDirectiveLocs.back().getLoc(), |
71 | 101 | DirLoc.getLoc())); |
72 | 101 | CondDirectiveLocs.push_back(DirLoc); |
73 | 101 | } |
74 | | |
75 | | void PPConditionalDirectiveRecord::If(SourceLocation Loc, |
76 | | SourceRange ConditionRange, |
77 | 52 | ConditionValueKind ConditionValue) { |
78 | 52 | addCondDirectiveLoc(CondDirectiveLoc(Loc, CondDirectiveStack.back())); |
79 | 52 | CondDirectiveStack.push_back(Loc); |
80 | 52 | } |
81 | | |
82 | | void PPConditionalDirectiveRecord::Ifdef(SourceLocation Loc, |
83 | | const Token &MacroNameTok, |
84 | 17 | const MacroDefinition &MD) { |
85 | 17 | addCondDirectiveLoc(CondDirectiveLoc(Loc, CondDirectiveStack.back())); |
86 | 17 | CondDirectiveStack.push_back(Loc); |
87 | 17 | } |
88 | | |
89 | | void PPConditionalDirectiveRecord::Ifndef(SourceLocation Loc, |
90 | | const Token &MacroNameTok, |
91 | 11 | const MacroDefinition &MD) { |
92 | 11 | addCondDirectiveLoc(CondDirectiveLoc(Loc, CondDirectiveStack.back())); |
93 | 11 | CondDirectiveStack.push_back(Loc); |
94 | 11 | } |
95 | | |
96 | | void PPConditionalDirectiveRecord::Elif(SourceLocation Loc, |
97 | | SourceRange ConditionRange, |
98 | | ConditionValueKind ConditionValue, |
99 | 1 | SourceLocation IfLoc) { |
100 | 1 | addCondDirectiveLoc(CondDirectiveLoc(Loc, CondDirectiveStack.back())); |
101 | 1 | CondDirectiveStack.back() = Loc; |
102 | 1 | } |
103 | | |
104 | | void PPConditionalDirectiveRecord::Elifdef(SourceLocation Loc, const Token &, |
105 | 0 | const MacroDefinition &) { |
106 | 0 | addCondDirectiveLoc(CondDirectiveLoc(Loc, CondDirectiveStack.back())); |
107 | 0 | CondDirectiveStack.back() = Loc; |
108 | 0 | } |
109 | | void PPConditionalDirectiveRecord::Elifdef(SourceLocation Loc, SourceRange, |
110 | 0 | SourceLocation) { |
111 | 0 | addCondDirectiveLoc(CondDirectiveLoc(Loc, CondDirectiveStack.back())); |
112 | 0 | CondDirectiveStack.back() = Loc; |
113 | 0 | } |
114 | | |
115 | | void PPConditionalDirectiveRecord::Elifndef(SourceLocation Loc, const Token &, |
116 | 0 | const MacroDefinition &) { |
117 | 0 | addCondDirectiveLoc(CondDirectiveLoc(Loc, CondDirectiveStack.back())); |
118 | 0 | CondDirectiveStack.back() = Loc; |
119 | 0 | } |
120 | | void PPConditionalDirectiveRecord::Elifndef(SourceLocation Loc, SourceRange, |
121 | 0 | SourceLocation) { |
122 | 0 | addCondDirectiveLoc(CondDirectiveLoc(Loc, CondDirectiveStack.back())); |
123 | 0 | CondDirectiveStack.back() = Loc; |
124 | 0 | } |
125 | | |
126 | | void PPConditionalDirectiveRecord::Else(SourceLocation Loc, |
127 | 9 | SourceLocation IfLoc) { |
128 | 9 | addCondDirectiveLoc(CondDirectiveLoc(Loc, CondDirectiveStack.back())); |
129 | 9 | CondDirectiveStack.back() = Loc; |
130 | 9 | } |
131 | | |
132 | | void PPConditionalDirectiveRecord::Endif(SourceLocation Loc, |
133 | 80 | SourceLocation IfLoc) { |
134 | 80 | addCondDirectiveLoc(CondDirectiveLoc(Loc, CondDirectiveStack.back())); |
135 | 80 | assert(!CondDirectiveStack.empty()); |
136 | 80 | CondDirectiveStack.pop_back(); |
137 | 80 | } |
138 | | |
139 | 0 | size_t PPConditionalDirectiveRecord::getTotalMemory() const { |
140 | 0 | return llvm::capacity_in_bytes(CondDirectiveLocs); |
141 | 0 | } |