Coverage Report

Created: 2019-07-24 05:18

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