Coverage Report

Created: 2018-09-25 23:22

/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
12.1M
  StoredDeclsList() = default;
50
51
8.87M
  StoredDeclsList(StoredDeclsList &&RHS) : Data(RHS.Data) {
52
8.87M
    RHS.Data = (NamedDecl *)nullptr;
53
8.87M
  }
54
55
9.68M
  ~StoredDeclsList() {
56
9.68M
    // If this is a vector-form, free the vector.
57
9.68M
    if (DeclsTy *Vector = getAsVector())
58
78.7k
      delete Vector;
59
9.68M
  }
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
62.6M
  bool isNull() const { return Data.isNull(); }
70
71
52.4M
  NamedDecl *getAsDecl() const {
72
52.4M
    return Data.dyn_cast<NamedDecl *>();
73
52.4M
  }
74
75
17.7M
  DeclsAndHasExternalTy getAsVectorAndHasExternal() const {
76
17.7M
    return Data.dyn_cast<DeclsAndHasExternalTy>();
77
17.7M
  }
78
79
17.6M
  DeclsTy *getAsVector() const {
80
17.6M
    return getAsVectorAndHasExternal().getPointer();
81
17.6M
  }
82
83
96.7k
  bool hasExternalDecls() const {
84
96.7k
    return getAsVectorAndHasExternal().getInt();
85
96.7k
  }
86
87
17.0k
  void setHasExternalDecls() {
88
17.0k
    if (DeclsTy *Vec = getAsVector())
89
5.11k
      Data = DeclsAndHasExternalTy(Vec, true);
90
11.9k
    else {
91
11.9k
      DeclsTy *VT = new DeclsTy();
92
11.9k
      if (NamedDecl *OldD = getAsDecl())
93
1.45k
        VT->push_back(OldD);
94
11.9k
      Data = DeclsAndHasExternalTy(VT, true);
95
11.9k
    }
96
17.0k
  }
97
98
12.4M
  void setOnlyValue(NamedDecl *ND) {
99
12.4M
    assert(!getAsVector() && "Not inline");
100
12.4M
    Data = ND;
101
12.4M
    // Make sure that Data is a plain NamedDecl* so we can use its address
102
12.4M
    // at getLookupResult.
103
12.4M
    assert(*(NamedDecl **)&Data == ND &&
104
12.4M
           "PointerUnion mangles the NamedDecl pointer!");
105
12.4M
  }
106
107
203
  void remove(NamedDecl *D) {
108
203
    assert(!isNull() && "removing from empty list");
109
203
    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
182
116
182
    DeclsTy &Vec = *getAsVector();
117
182
    DeclsTy::iterator I = std::find(Vec.begin(), Vec.end(), D);
118
182
    assert(I != Vec.end() && "list does not contain decl");
119
182
    Vec.erase(I);
120
182
121
182
    assert(std::find(Vec.begin(), Vec.end(), D)
122
182
             == Vec.end() && "list still contains decl");
123
182
  }
124
125
  /// Remove any declarations which were imported from an external
126
  /// AST source.
127
31.4k
  void removeExternalDecls() {
128
31.4k
    if (isNull()) {
129
25.2k
      // Nothing to do.
130
25.2k
    } else 
if (NamedDecl *6.28k
Singleton6.28k
= 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.4k
  }
142
143
  /// getLookupResult - Return an array of all the decls that this list
144
  /// represents.
145
48.0M
  DeclContext::lookup_result getLookupResult() {
146
48.0M
    if (isNull())
147
29.6k
      return DeclContext::lookup_result();
148
48.0M
149
48.0M
    // If we have a single NamedDecl, return it.
150
48.0M
    if (NamedDecl *ND = getAsDecl()) {
151
43.1M
      assert(!isNull() && "Empty list isn't allowed");
152
43.1M
153
43.1M
      // Data is a raw pointer to a NamedDecl*, return it.
154
43.1M
      return DeclContext::lookup_result(ND);
155
43.1M
    }
156
4.92M
157
4.92M
    assert(getAsVector() && "Must have a vector at this point");
158
4.92M
    DeclsTy &Vector = *getAsVector();
159
4.92M
160
4.92M
    // Otherwise, we have a range result.
161
4.92M
    return DeclContext::lookup_result(Vector);
162
4.92M
  }
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.45M
  bool HandleRedeclaration(NamedDecl *D, bool IsKnownNewer) {
167
2.45M
    // Most decls only have one entry in their list, special case it.
168
2.45M
    if (NamedDecl *OldD = getAsDecl()) {
169
1.36M
      if (!D->declarationReplaces(OldD, IsKnownNewer))
170
986k
        return false;
171
376k
      setOnlyValue(D);
172
376k
      return true;
173
376k
    }
174
1.09M
175
1.09M
    // Determine if this declaration is actually a redeclaration.
176
1.09M
    DeclsTy &Vec = *getAsVector();
177
1.09M
    for (DeclsTy::iterator OD = Vec.begin(), ODEnd = Vec.end();
178
8.78M
         OD != ODEnd; 
++OD7.69M
) {
179
7.87M
      NamedDecl *OldD = *OD;
180
7.87M
      if (D->declarationReplaces(OldD, IsKnownNewer)) {
181
178k
        *OD = D;
182
178k
        return true;
183
178k
      }
184
7.87M
    }
185
1.09M
186
1.09M
    
return false918k
;
187
1.09M
  }
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.92M
  void AddSubsequentDecl(NamedDecl *D) {
192
1.92M
    assert(!isNull() && "don't AddSubsequentDecl when we have no decls");
193
1.92M
194
1.92M
    // If this is the second decl added to the list, convert this to vector
195
1.92M
    // form.
196
1.92M
    if (NamedDecl *OldD = getAsDecl()) {
197
988k
      DeclsTy *VT = new DeclsTy();
198
988k
      VT->push_back(OldD);
199
988k
      Data = DeclsAndHasExternalTy(VT, false);
200
988k
    }
201
1.92M
202
1.92M
    DeclsTy &Vec = *getAsVector();
203
1.92M
204
1.92M
    // Using directives end up in a special entry which contains only
205
1.92M
    // other using directives, so all this logic is wasted for them.
206
1.92M
    // But avoiding the logic wastes time in the far-more-common case
207
1.92M
    // that we're *not* adding a new using directive.
208
1.92M
209
1.92M
    // Tag declarations always go at the end of the list so that an
210
1.92M
    // iterator which points at the first tag will start a span of
211
1.92M
    // decls that only contains tags.
212
1.92M
    if (D->hasTagIdentifierNamespace())
213
4.56k
      Vec.push_back(D);
214
1.91M
215
1.91M
    // Resolved using declarations go at the front of the list so that
216
1.91M
    // they won't show up in other lookup results.  Unresolved using
217
1.91M
    // declarations (which are always in IDNS_Using | IDNS_Ordinary)
218
1.91M
    // follow that so that the using declarations will be contiguous.
219
1.91M
    else if (D->getIdentifierNamespace() & Decl::IDNS_Using) {
220
1.40k
      DeclsTy::iterator I = Vec.begin();
221
1.40k
      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.40k
      Vec.insert(I, D);
227
1.40k
228
1.40k
    // All other declarations go at the end of the list, but before any
229
1.40k
    // tag declarations.  But we can be clever about tag declarations
230
1.40k
    // because there can only ever be one in a scope.
231
1.91M
    } else if (!Vec.empty() && 
Vec.back()->hasTagIdentifierNamespace()1.90M
) {
232
6.32k
      NamedDecl *TagD = Vec.back();
233
6.32k
      Vec.back() = D;
234
6.32k
      Vec.push_back(TagD);
235
6.32k
    } else
236
1.90M
      Vec.push_back(D);
237
1.92M
  }
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
412k
  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