Coverage Report

Created: 2020-02-18 08: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
45
  : SourceMgr(SM) {
20
45
  CondDirectiveStack.push_back(SourceLocation());
21
45
}
22
23
bool PPConditionalDirectiveRecord::rangeIntersectsConditionalDirective(
24
1.22k
                                                      SourceRange Range) const {
25
1.22k
  if (Range.isInvalid())
26
0
    return false;
27
1.22k
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
112
33
112
  if (SourceMgr.isBeforeInTranslationUnit(Range.getEnd(), low->getLoc()))
34
94
    return false;
35
18
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
18
43
18
  return low->getRegionLoc() != uppRegion;
44
18
}
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
73
53
73
  if (SourceMgr.isBeforeInTranslationUnit(CondDirectiveLocs.back().getLoc(),
54
73
                                          Loc))
55
39
    return CondDirectiveStack.back();
56
34
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
101
                                                      CondDirectiveLoc DirLoc) {
65
101
  // Ignore directives in system headers.
66
101
  if (SourceMgr.isInSystemHeader(DirLoc.getLoc()))
67
0
    return;
68
101
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
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::Else(SourceLocation Loc,
105
9
                                        SourceLocation IfLoc) {
106
9
  addCondDirectiveLoc(CondDirectiveLoc(Loc, CondDirectiveStack.back()));
107
9
  CondDirectiveStack.back() = Loc;
108
9
}
109
110
void PPConditionalDirectiveRecord::Endif(SourceLocation Loc,
111
46
                                         SourceLocation IfLoc) {
112
46
  addCondDirectiveLoc(CondDirectiveLoc(Loc, CondDirectiveStack.back()));
113
46
  assert(!CondDirectiveStack.empty());
114
46
  CondDirectiveStack.pop_back();
115
46
}
116
117
0
size_t PPConditionalDirectiveRecord::getTotalMemory() const {
118
0
  return llvm::capacity_in_bytes(CondDirectiveLocs);
119
0
}