Coverage Report

Created: 2018-12-11 17:59

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/include/clang/AST/DeclContextInternals.h
Line
Count
Source (jump to first uncovered line)
1
//===- DeclContextInternals.h - DeclContext Representation ------*- 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 defines the data structures used in the implementation
11
//  of DeclContext.
12
//
13
//===----------------------------------------------------------------------===//
14
15
#ifndef LLVM_CLANG_AST_DECLCONTEXTINTERNALS_H
16
#define LLVM_CLANG_AST_DECLCONTEXTINTERNALS_H
17
18
#include "clang/AST/Decl.h"
19
#include "clang/AST/DeclBase.h"
20
#include "clang/AST/DeclCXX.h"
21
#include "clang/AST/DeclarationName.h"
22
#include "llvm/ADT/DenseMap.h"
23
#include "llvm/ADT/PointerIntPair.h"
24
#include "llvm/ADT/PointerUnion.h"
25
#include "llvm/ADT/SmallVector.h"
26
#include <algorithm>
27
#include <cassert>
28
29
namespace clang {
30
31
class DependentDiagnostic;
32
33
/// An array of decls optimized for the common case of only containing
34
/// one entry.
35
struct StoredDeclsList {
36
  /// When in vector form, this is what the Data pointer points to.
37
  using DeclsTy = SmallVector<NamedDecl *, 4>;
38
39
  /// A collection of declarations, with a flag to indicate if we have
40
  /// further external declarations.
41
  using DeclsAndHasExternalTy = llvm::PointerIntPair<DeclsTy *, 1, bool>;
42
43
  /// The stored data, which will be either a pointer to a NamedDecl,
44
  /// or a pointer to a vector with a flag to indicate if there are further
45
  /// external declarations.
46
  llvm::PointerUnion<NamedDecl *, DeclsAndHasExternalTy> Data;
47
48
public:
49
17.2M
  StoredDeclsList() = default;
50
51
12.7M
  StoredDeclsList(StoredDeclsList &&RHS) : Data(RHS.Data) {
52
12.7M
    RHS.Data = (NamedDecl *)nullptr;
53
12.7M
  }
54
55
13.5M
  ~StoredDeclsList() {
56
13.5M
    // If this is a vector-form, free the vector.
57
13.5M
    if (DeclsTy *Vector = getAsVector())
58
80.6k
      delete Vector;
59
13.5M
  }
60
61
21
  StoredDeclsList &operator=(StoredDeclsList &&RHS) {
62
21
    if (DeclsTy *Vector = getAsVector())
63
0
      delete Vector;
64
21
    Data = RHS.Data;
65
21
    RHS.Data = (NamedDecl *)nullptr;
66
21
    return *this;
67
21
  }
68
69
88.7M
  bool isNull() const { return Data.isNull(); }
70
71
74.0M
  NamedDecl *getAsDecl() const {
72
74.0M
    return Data.dyn_cast<NamedDecl *>();
73
74.0M
  }
74
75
24.2M
  DeclsAndHasExternalTy getAsVectorAndHasExternal() const {
76
24.2M
    return Data.dyn_cast<DeclsAndHasExternalTy>();
77
24.2M
  }
78
79
24.1M
  DeclsTy *getAsVector() const {
80
24.1M
    return getAsVectorAndHasExternal().getPointer();
81
24.1M
  }
82
83
97.1k
  bool hasExternalDecls() const {
84
97.1k
    return getAsVectorAndHasExternal().getInt();
85
97.1k
  }
86
87
17.1k
  void setHasExternalDecls() {
88
17.1k
    if (DeclsTy *Vec = getAsVector())
89
5.12k
      Data = DeclsAndHasExternalTy(Vec, true);
90
12.0k
    else {
91
12.0k
      DeclsTy *VT = new DeclsTy();
92
12.0k
      if (NamedDecl *OldD = getAsDecl())
93
1.48k
        VT->push_back(OldD);
94
12.0k
      Data = DeclsAndHasExternalTy(VT, true);
95
12.0k
    }
96
17.1k
  }
97
98
17.7M
  void setOnlyValue(NamedDecl *ND) {
99
17.7M
    assert(!getAsVector() && "Not inline");
100
17.7M
    Data = ND;
101
17.7M
    // Make sure that Data is a plain NamedDecl* so we can use its address
102
17.7M
    // at getLookupResult.
103
17.7M
    assert(*(NamedDecl **)&Data == ND &&
104
17.7M
           "PointerUnion mangles the NamedDecl pointer!");
105
17.7M
  }
106
107
212
  void remove(NamedDecl *D) {
108
212
    assert(!isNull() && "removing from empty list");
109
212
    if (NamedDecl *Singleton = getAsDecl()) {
110
21
      assert(Singleton == D && "list is different singleton");
111
21
      (void)Singleton;
112
21
      Data = (NamedDecl *)nullptr;
113
21
      return;
114
21
    }
115
191
116
191
    DeclsTy &Vec = *getAsVector();
117
191
    DeclsTy::iterator I = std::find(Vec.begin(), Vec.end(), D);
118
191
    assert(I != Vec.end() && "list does not contain decl");
119
191
    Vec.erase(I);
120
191
121
191
    assert(std::find(Vec.begin(), Vec.end(), D)
122
191
             == Vec.end() && "list still contains decl");
123
191
  }
124
125
  /// Remove any declarations which were imported from an external
126
  /// AST source.
127
31.6k
  void removeExternalDecls() {
128
31.6k
    if (isNull()) {
129
25.3k
      // Nothing to do.
130
25.3k
    } else 
if (NamedDecl *6.29k
Singleton6.29k
= getAsDecl()) {
131
23
      if (Singleton->isFromASTFile())
132
21
        *this = StoredDeclsList();
133
6.26k
    } else {
134
6.26k
      DeclsTy &Vec = *getAsVector();
135
6.26k
      Vec.erase(std::remove_if(Vec.begin(), Vec.end(),
136
9.07k
                               [](Decl *D) { return D->isFromASTFile(); }),
137
6.26k
                Vec.end());
138
6.26k
      // Don't have any external decls any more.
139
6.26k
      Data = DeclsAndHasExternalTy(&Vec, false);
140
6.26k
    }
141
31.6k
  }
142
143
  /// getLookupResult - Return an array of all the decls that this list
144
  /// represents.
145
68.2M
  DeclContext::lookup_result getLookupResult() {
146
68.2M
    if (isNull())
147
29.7k
      return DeclContext::lookup_result();
148
68.1M
149
68.1M
    // If we have a single NamedDecl, return it.
150
68.1M
    if (NamedDecl *ND = getAsDecl()) {
151
61.6M
      assert(!isNull() && "Empty list isn't allowed");
152
61.6M
153
61.6M
      // Data is a raw pointer to a NamedDecl*, return it.
154
61.6M
      return DeclContext::lookup_result(ND);
155
61.6M
    }
156
6.56M
157
6.56M
    assert(getAsVector() && "Must have a vector at this point");
158
6.56M
    DeclsTy &Vector = *getAsVector();
159
6.56M
160
6.56M
    // Otherwise, we have a range result.
161
6.56M
    return DeclContext::lookup_result(Vector);
162
6.56M
  }
163
164
  /// HandleRedeclaration - If this is a redeclaration of an existing decl,
165
  /// replace the old one with D and return true.  Otherwise return false.
166
3.30M
  bool HandleRedeclaration(NamedDecl *D, bool IsKnownNewer) {
167
3.30M
    // Most decls only have one entry in their list, special case it.
168
3.30M
    if (NamedDecl *OldD = getAsDecl()) {
169
1.86M
      if (!D->declarationReplaces(OldD, IsKnownNewer))
170
1.33M
        return false;
171
528k
      setOnlyValue(D);
172
528k
      return true;
173
528k
    }
174
1.44M
175
1.44M
    // Determine if this declaration is actually a redeclaration.
176
1.44M
    DeclsTy &Vec = *getAsVector();
177
1.44M
    for (DeclsTy::iterator OD = Vec.begin(), ODEnd = Vec.end();
178
11.0M
         OD != ODEnd; 
++OD9.56M
) {
179
9.80M
      NamedDecl *OldD = *OD;
180
9.80M
      if (D->declarationReplaces(OldD, IsKnownNewer)) {
181
245k
        *OD = D;
182
245k
        return true;
183
245k
      }
184
9.80M
    }
185
1.44M
186
1.44M
    
return false1.19M
;
187
1.44M
  }
188
189
  /// AddSubsequentDecl - This is called on the second and later decl when it is
190
  /// not a redeclaration to merge it into the appropriate place in our list.
191
2.55M
  void AddSubsequentDecl(NamedDecl *D) {
192
2.55M
    assert(!isNull() && "don't AddSubsequentDecl when we have no decls");
193
2.55M
194
2.55M
    // If this is the second decl added to the list, convert this to vector
195
2.55M
    // form.
196
2.55M
    if (NamedDecl *OldD = getAsDecl()) {
197
1.34M
      DeclsTy *VT = new DeclsTy();
198
1.34M
      VT->push_back(OldD);
199
1.34M
      Data = DeclsAndHasExternalTy(VT, false);
200
1.34M
    }
201
2.55M
202
2.55M
    DeclsTy &Vec = *getAsVector();
203
2.55M
204
2.55M
    // Using directives end up in a special entry which contains only
205
2.55M
    // other using directives, so all this logic is wasted for them.
206
2.55M
    // But avoiding the logic wastes time in the far-more-common case
207
2.55M
    // that we're *not* adding a new using directive.
208
2.55M
209
2.55M
    // Tag declarations always go at the end of the list so that an
210
2.55M
    // iterator which points at the first tag will start a span of
211
2.55M
    // decls that only contains tags.
212
2.55M
    if (D->hasTagIdentifierNamespace())
213
4.98k
      Vec.push_back(D);
214
2.54M
215
2.54M
    // Resolved using declarations go at the front of the list so that
216
2.54M
    // they won't show up in other lookup results.  Unresolved using
217
2.54M
    // declarations (which are always in IDNS_Using | IDNS_Ordinary)
218
2.54M
    // follow that so that the using declarations will be contiguous.
219
2.54M
    else if (D->getIdentifierNamespace() & Decl::IDNS_Using) {
220
1.61k
      DeclsTy::iterator I = Vec.begin();
221
1.61k
      if (D->getIdentifierNamespace() != Decl::IDNS_Using) {
222
87
        while (I != Vec.end() &&
223
87
               
(*I)->getIdentifierNamespace() == Decl::IDNS_Using45
)
224
1
          ++I;
225
86
      }
226
1.61k
      Vec.insert(I, D);
227
1.61k
228
1.61k
    // All other declarations go at the end of the list, but before any
229
1.61k
    // tag declarations.  But we can be clever about tag declarations
230
1.61k
    // because there can only ever be one in a scope.
231
2.54M
    } else if (!Vec.empty() && 
Vec.back()->hasTagIdentifierNamespace()2.53M
) {
232
8.31k
      NamedDecl *TagD = Vec.back();
233
8.31k
      Vec.back() = D;
234
8.31k
      Vec.push_back(TagD);
235
8.31k
    } else
236
2.53M
      Vec.push_back(D);
237
2.55M
  }
238
};
239
240
class StoredDeclsMap
241
    : public llvm::SmallDenseMap<DeclarationName, StoredDeclsList, 4> {
242
public:
243
  static void DestroyAll(StoredDeclsMap *Map, bool Dependent);
244
245
private:
246
  friend class ASTContext; // walks the chain deleting these
247
  friend class DeclContext;
248
249
  llvm::PointerIntPair<StoredDeclsMap*, 1> Previous;
250
};
251
252
class DependentStoredDeclsMap : public StoredDeclsMap {
253
public:
254
534k
  DependentStoredDeclsMap() = default;
255
256
private:
257
  friend class DeclContext; // iterates over diagnostics
258
  friend class DependentDiagnostic;
259
260
  DependentDiagnostic *FirstDiagnostic = nullptr;
261
};
262
263
} // namespace clang
264
265
#endif // LLVM_CLANG_AST_DECLCONTEXTINTERNALS_H