Coverage Report

Created: 2018-07-21 08:31

/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
11.3M
  StoredDeclsList() = default;
50
51
8.48M
  StoredDeclsList(StoredDeclsList &&RHS) : Data(RHS.Data) {
52
8.48M
    RHS.Data = (NamedDecl *)nullptr;
53
8.48M
  }
54
55
9.27M
  ~StoredDeclsList() {
56
9.27M
    // If this is a vector-form, free the vector.
57
9.27M
    if (DeclsTy *Vector = getAsVector())
58
75.0k
      delete Vector;
59
9.27M
  }
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
57.5M
  bool isNull() const { return Data.isNull(); }
70
71
47.9M
  NamedDecl *getAsDecl() const {
72
47.9M
    return Data.dyn_cast<NamedDecl *>();
73
47.9M
  }
74
75
16.5M
  DeclsAndHasExternalTy getAsVectorAndHasExternal() const {
76
16.5M
    return Data.dyn_cast<DeclsAndHasExternalTy>();
77
16.5M
  }
78
79
16.4M
  DeclsTy *getAsVector() const {
80
16.4M
    return getAsVectorAndHasExternal().getPointer();
81
16.4M
  }
82
83
93.8k
  bool hasExternalDecls() const {
84
93.8k
    return getAsVectorAndHasExternal().getInt();
85
93.8k
  }
86
87
16.0k
  void setHasExternalDecls() {
88
16.0k
    if (DeclsTy *Vec = getAsVector())
89
4.87k
      Data = DeclsAndHasExternalTy(Vec, true);
90
11.1k
    else {
91
11.1k
      DeclsTy *VT = new DeclsTy();
92
11.1k
      if (NamedDecl *OldD = getAsDecl())
93
1.39k
        VT->push_back(OldD);
94
11.1k
      Data = DeclsAndHasExternalTy(VT, true);
95
11.1k
    }
96
16.0k
  }
97
98
11.6M
  void setOnlyValue(NamedDecl *ND) {
99
11.6M
    assert(!getAsVector() && "Not inline");
100
11.6M
    Data = ND;
101
11.6M
    // Make sure that Data is a plain NamedDecl* so we can use its address
102
11.6M
    // at getLookupResult.
103
11.6M
    assert(*(NamedDecl **)&Data == ND &&
104
11.6M
           "PointerUnion mangles the NamedDecl pointer!");
105
11.6M
  }
106
107
163
  void remove(NamedDecl *D) {
108
163
    assert(!isNull() && "removing from empty list");
109
163
    if (NamedDecl *Singleton = getAsDecl()) {
110
1
      assert(Singleton == D && "list is different singleton");
111
1
      (void)Singleton;
112
1
      Data = (NamedDecl *)nullptr;
113
1
      return;
114
1
    }
115
162
116
162
    DeclsTy &Vec = *getAsVector();
117
162
    DeclsTy::iterator I = std::find(Vec.begin(), Vec.end(), D);
118
162
    assert(I != Vec.end() && "list does not contain decl");
119
162
    Vec.erase(I);
120
162
121
162
    assert(std::find(Vec.begin(), Vec.end(), D)
122
162
             == Vec.end() && "list still contains decl");
123
162
  }
124
125
  /// Remove any declarations which were imported from an external
126
  /// AST source.
127
30.8k
  void removeExternalDecls() {
128
30.8k
    if (isNull()) {
129
24.9k
      // Nothing to do.
130
24.9k
    } else 
if (NamedDecl *5.91k
Singleton5.91k
= getAsDecl()) {
131
22
      if (Singleton->isFromASTFile())
132
21
        *this = StoredDeclsList();
133
5.88k
    } else {
134
5.88k
      DeclsTy &Vec = *getAsVector();
135
5.88k
      Vec.erase(std::remove_if(Vec.begin(), Vec.end(),
136
8.68k
                               [](Decl *D) { return D->isFromASTFile(); }),
137
5.88k
                Vec.end());
138
5.88k
      // Don't have any external decls any more.
139
5.88k
      Data = DeclsAndHasExternalTy(&Vec, false);
140
5.88k
    }
141
30.8k
  }
142
143
  /// getLookupResult - Return an array of all the decls that this list
144
  /// represents.
145
43.9M
  DeclContext::lookup_result getLookupResult() {
146
43.9M
    if (isNull())
147
27.8k
      return DeclContext::lookup_result();
148
43.9M
149
43.9M
    // If we have a single NamedDecl, return it.
150
43.9M
    if (NamedDecl *ND = getAsDecl()) {
151
39.5M
      assert(!isNull() && "Empty list isn't allowed");
152
39.5M
153
39.5M
      // Data is a raw pointer to a NamedDecl*, return it.
154
39.5M
      return DeclContext::lookup_result(ND);
155
39.5M
    }
156
4.42M
157
4.42M
    assert(getAsVector() && "Must have a vector at this point");
158
4.42M
    DeclsTy &Vector = *getAsVector();
159
4.42M
160
4.42M
    // Otherwise, we have a range result.
161
4.42M
    return DeclContext::lookup_result(Vector);
162
4.42M
  }
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
2.23M
  bool HandleRedeclaration(NamedDecl *D, bool IsKnownNewer) {
167
2.23M
    // Most decls only have one entry in their list, special case it.
168
2.23M
    if (NamedDecl *OldD = getAsDecl()) {
169
1.25M
      if (!D->declarationReplaces(OldD, IsKnownNewer))
170
906k
        return false;
171
351k
      setOnlyValue(D);
172
351k
      return true;
173
351k
    }
174
976k
175
976k
    // Determine if this declaration is actually a redeclaration.
176
976k
    DeclsTy &Vec = *getAsVector();
177
976k
    for (DeclsTy::iterator OD = Vec.begin(), ODEnd = Vec.end();
178
7.98M
         OD != ODEnd; 
++OD7.00M
) {
179
7.16M
      NamedDecl *OldD = *OD;
180
7.16M
      if (D->declarationReplaces(OldD, IsKnownNewer)) {
181
157k
        *OD = D;
182
157k
        return true;
183
157k
      }
184
7.16M
    }
185
976k
186
976k
    
return false818k
;
187
976k
  }
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
1.74M
  void AddSubsequentDecl(NamedDecl *D) {
192
1.74M
    assert(!isNull() && "don't AddSubsequentDecl when we have no decls");
193
1.74M
194
1.74M
    // If this is the second decl added to the list, convert this to vector
195
1.74M
    // form.
196
1.74M
    if (NamedDecl *OldD = getAsDecl()) {
197
908k
      DeclsTy *VT = new DeclsTy();
198
908k
      VT->push_back(OldD);
199
908k
      Data = DeclsAndHasExternalTy(VT, false);
200
908k
    }
201
1.74M
202
1.74M
    DeclsTy &Vec = *getAsVector();
203
1.74M
204
1.74M
    // Using directives end up in a special entry which contains only
205
1.74M
    // other using directives, so all this logic is wasted for them.
206
1.74M
    // But avoiding the logic wastes time in the far-more-common case
207
1.74M
    // that we're *not* adding a new using directive.
208
1.74M
209
1.74M
    // Tag declarations always go at the end of the list so that an
210
1.74M
    // iterator which points at the first tag will start a span of
211
1.74M
    // decls that only contains tags.
212
1.74M
    if (D->hasTagIdentifierNamespace())
213
4.19k
      Vec.push_back(D);
214
1.73M
215
1.73M
    // Resolved using declarations go at the front of the list so that
216
1.73M
    // they won't show up in other lookup results.  Unresolved using
217
1.73M
    // declarations (which are always in IDNS_Using | IDNS_Ordinary)
218
1.73M
    // follow that so that the using declarations will be contiguous.
219
1.73M
    else if (D->getIdentifierNamespace() & Decl::IDNS_Using) {
220
1.36k
      DeclsTy::iterator I = Vec.begin();
221
1.36k
      if (D->getIdentifierNamespace() != Decl::IDNS_Using) {
222
85
        while (I != Vec.end() &&
223
85
               
(*I)->getIdentifierNamespace() == Decl::IDNS_Using45
)
224
1
          ++I;
225
84
      }
226
1.36k
      Vec.insert(I, D);
227
1.36k
228
1.36k
    // All other declarations go at the end of the list, but before any
229
1.36k
    // tag declarations.  But we can be clever about tag declarations
230
1.36k
    // because there can only ever be one in a scope.
231
1.73M
    } else if (!Vec.empty() && 
Vec.back()->hasTagIdentifierNamespace()1.72M
) {
232
5.52k
      NamedDecl *TagD = Vec.back();
233
5.52k
      Vec.back() = D;
234
5.52k
      Vec.push_back(TagD);
235
5.52k
    } else
236
1.73M
      Vec.push_back(D);
237
1.74M
  }
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
390k
  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