Coverage Report

Created: 2021-06-15 06:44

/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
46
  : SourceMgr(SM) {
20
46
  CondDirectiveStack.push_back(SourceLocation());
21
46
}
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
0
  return low->getRegionLoc();
61
73
}
62
63
void PPConditionalDirectiveRecord::addCondDirectiveLoc(
64
101
                                                      CondDirectiveLoc DirLoc) {
65
  // Ignore directives in system headers.
66
101
  if (SourceMgr.isInSystemHeader(DirLoc.getLoc()))
67
0
    return;
68
69
101
  assert(CondDirectiveLocs.empty() ||
70
101
         SourceMgr.isBeforeInTranslationUnit(CondDirectiveLocs.back().getLoc(),
71
101
                                             DirLoc.getLoc()));
72
0
  CondDirectiveLocs.push_back(DirLoc);
73
101
}
74
75
void PPConditionalDirectiveRecord::If(SourceLocation Loc,
76
                                      SourceRange ConditionRange,
77
33
                                      ConditionValueKind ConditionValue) {
78
33
  addCondDirectiveLoc(CondDirectiveLoc(Loc, CondDirectiveStack.back()));
79
33
  CondDirectiveStack.push_back(Loc);
80
33
}
81
82
void PPConditionalDirectiveRecord::Ifdef(SourceLocation Loc,
83
                                         const Token &MacroNameTok,
84
4
                                         const MacroDefinition &MD) {
85
4
  addCondDirectiveLoc(CondDirectiveLoc(Loc, CondDirectiveStack.back()));
86
4
  CondDirectiveStack.push_back(Loc);
87
4
}
88
89
void PPConditionalDirectiveRecord::Ifndef(SourceLocation Loc,
90
                                          const Token &MacroNameTok,
91
9
                                          const MacroDefinition &MD) {
92
9
  addCondDirectiveLoc(CondDirectiveLoc(Loc, CondDirectiveStack.back()));
93
9
  CondDirectiveStack.push_back(Loc);
94
9
}
95
96
void PPConditionalDirectiveRecord::Elif(SourceLocation Loc,
97
                                        SourceRange ConditionRange,
98
                                        ConditionValueKind ConditionValue,
99
0
                                        SourceLocation IfLoc) {
100
0
  addCondDirectiveLoc(CondDirectiveLoc(Loc, CondDirectiveStack.back()));
101
0
  CondDirectiveStack.back() = Loc;
102
0
}
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
46
                                         SourceLocation IfLoc) {
134
46
  addCondDirectiveLoc(CondDirectiveLoc(Loc, CondDirectiveStack.back()));
135
46
  assert(!CondDirectiveStack.empty());
136
0
  CondDirectiveStack.pop_back();
137
46
}
138
139
0
size_t PPConditionalDirectiveRecord::getTotalMemory() const {
140
0
  return llvm::capacity_in_bytes(CondDirectiveLocs);
141
0
}