Coverage Report

Created: 2019-03-22 08:08

/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
12.0M
  StoredDeclsList() = default;
49
50
8.68M
  StoredDeclsList(StoredDeclsList &&RHS) : Data(RHS.Data) {
51
8.68M
    RHS.Data = (NamedDecl *)nullptr;
52
8.68M
  }
53
54
9.54M
  ~StoredDeclsList() {
55
9.54M
    // If this is a vector-form, free the vector.
56
9.54M
    if (DeclsTy *Vector = getAsVector())
57
82.7k
      delete Vector;
58
9.54M
  }
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
62.9M
  bool isNull() const { return Data.isNull(); }
69
70
52.9M
  NamedDecl *getAsDecl() const {
71
52.9M
    return Data.dyn_cast<NamedDecl *>();
72
52.9M
  }
73
74
17.7M
  DeclsAndHasExternalTy getAsVectorAndHasExternal() const {
75
17.7M
    return Data.dyn_cast<DeclsAndHasExternalTy>();
76
17.7M
  }
77
78
17.6M
  DeclsTy *getAsVector() const {
79
17.6M
    return getAsVectorAndHasExternal().getPointer();
80
17.6M
  }
81
82
99.6k
  bool hasExternalDecls() const {
83
99.6k
    return getAsVectorAndHasExternal().getInt();
84
99.6k
  }
85
86
16.1k
  void setHasExternalDecls() {
87
16.1k
    if (DeclsTy *Vec = getAsVector())
88
4.93k
      Data = DeclsAndHasExternalTy(Vec, true);
89
11.2k
    else {
90
11.2k
      DeclsTy *VT = new DeclsTy();
91
11.2k
      if (NamedDecl *OldD = getAsDecl())
92
1.55k
        VT->push_back(OldD);
93
11.2k
      Data = DeclsAndHasExternalTy(VT, true);
94
11.2k
    }
95
16.1k
  }
96
97
12.2M
  void setOnlyValue(NamedDecl *ND) {
98
12.2M
    assert(!getAsVector() && "Not inline");
99
12.2M
    Data = ND;
100
12.2M
    // Make sure that Data is a plain NamedDecl* so we can use its address
101
12.2M
    // at getLookupResult.
102
12.2M
    assert(*(NamedDecl **)&Data == ND &&
103
12.2M
           "PointerUnion mangles the NamedDecl pointer!");
104
12.2M
  }
105
106
212
  void remove(NamedDecl *D) {
107
212
    assert(!isNull() && "removing from empty list");
108
212
    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
191
115
191
    DeclsTy &Vec = *getAsVector();
116
191
    DeclsTy::iterator I = std::find(Vec.begin(), Vec.end(), D);
117
191
    assert(I != Vec.end() && "list does not contain decl");
118
191
    Vec.erase(I);
119
191
120
191
    assert(std::find(Vec.begin(), Vec.end(), D)
121
191
             == Vec.end() && "list still contains decl");
122
191
  }
123
124
  /// Remove any declarations which were imported from an external
125
  /// AST source.
126
31.7k
  void removeExternalDecls() {
127
31.7k
    if (isNull()) {
128
25.2k
      // Nothing to do.
129
25.2k
    } else 
if (NamedDecl *6.43k
Singleton6.43k
= getAsDecl()) {
130
23
      if (Singleton->isFromASTFile())
131
21
        *this = StoredDeclsList();
132
6.40k
    } else {
133
6.40k
      DeclsTy &Vec = *getAsVector();
134
6.40k
      Vec.erase(std::remove_if(Vec.begin(), Vec.end(),
135
9.24k
                               [](Decl *D) { return D->isFromASTFile(); }),
136
6.40k
                Vec.end());
137
6.40k
      // Don't have any external decls any more.
138
6.40k
      Data = DeclsAndHasExternalTy(&Vec, false);
139
6.40k
    }
140
31.7k
  }
141
142
  /// getLookupResult - Return an array of all the decls that this list
143
  /// represents.
144
48.5M
  DeclContext::lookup_result getLookupResult() {
145
48.5M
    if (isNull())
146
30.0k
      return DeclContext::lookup_result();
147
48.4M
148
48.4M
    // If we have a single NamedDecl, return it.
149
48.4M
    if (NamedDecl *ND = getAsDecl()) {
150
43.4M
      assert(!isNull() && "Empty list isn't allowed");
151
43.4M
152
43.4M
      // Data is a raw pointer to a NamedDecl*, return it.
153
43.4M
      return DeclContext::lookup_result(ND);
154
43.4M
    }
155
5.03M
156
5.03M
    assert(getAsVector() && "Must have a vector at this point");
157
5.03M
    DeclsTy &Vector = *getAsVector();
158
5.03M
159
5.03M
    // Otherwise, we have a range result.
160
5.03M
    return DeclContext::lookup_result(Vector);
161
5.03M
  }
162
163
  /// HandleRedeclaration - If this is a redeclaration of an existing decl,
164
  /// replace the old one with D and return true.  Otherwise return false.
165
2.46M
  bool HandleRedeclaration(NamedDecl *D, bool IsKnownNewer) {
166
2.46M
    // Most decls only have one entry in their list, special case it.
167
2.46M
    if (NamedDecl *OldD = getAsDecl()) {
168
1.35M
      if (!D->declarationReplaces(OldD, IsKnownNewer))
169
1.00M
        return false;
170
352k
      setOnlyValue(D);
171
352k
      return true;
172
352k
    }
173
1.10M
174
1.10M
    // Determine if this declaration is actually a redeclaration.
175
1.10M
    DeclsTy &Vec = *getAsVector();
176
1.10M
    for (DeclsTy::iterator OD = Vec.begin(), ODEnd = Vec.end();
177
9.02M
         OD != ODEnd; 
++OD7.91M
) {
178
8.07M
      NamedDecl *OldD = *OD;
179
8.07M
      if (D->declarationReplaces(OldD, IsKnownNewer)) {
180
158k
        *OD = D;
181
158k
        return true;
182
158k
      }
183
8.07M
    }
184
1.10M
185
1.10M
    
return false948k
;
186
1.10M
  }
187
188
  /// AddSubsequentDecl - This is called on the second and later decl when it is
189
  /// not a redeclaration to merge it into the appropriate place in our list.
190
1.96M
  void AddSubsequentDecl(NamedDecl *D) {
191
1.96M
    assert(!isNull() && "don't AddSubsequentDecl when we have no decls");
192
1.96M
193
1.96M
    // If this is the second decl added to the list, convert this to vector
194
1.96M
    // form.
195
1.96M
    if (NamedDecl *OldD = getAsDecl()) {
196
1.00M
      DeclsTy *VT = new DeclsTy();
197
1.00M
      VT->push_back(OldD);
198
1.00M
      Data = DeclsAndHasExternalTy(VT, false);
199
1.00M
    }
200
1.96M
201
1.96M
    DeclsTy &Vec = *getAsVector();
202
1.96M
203
1.96M
    // Using directives end up in a special entry which contains only
204
1.96M
    // other using directives, so all this logic is wasted for them.
205
1.96M
    // But avoiding the logic wastes time in the far-more-common case
206
1.96M
    // that we're *not* adding a new using directive.
207
1.96M
208
1.96M
    // Tag declarations always go at the end of the list so that an
209
1.96M
    // iterator which points at the first tag will start a span of
210
1.96M
    // decls that only contains tags.
211
1.96M
    if (D->hasTagIdentifierNamespace())
212
4.34k
      Vec.push_back(D);
213
1.96M
214
1.96M
    // Resolved using declarations go at the front of the list so that
215
1.96M
    // they won't show up in other lookup results.  Unresolved using
216
1.96M
    // declarations (which are always in IDNS_Using | IDNS_Ordinary)
217
1.96M
    // follow that so that the using declarations will be contiguous.
218
1.96M
    else if (D->getIdentifierNamespace() & Decl::IDNS_Using) {
219
1.39k
      DeclsTy::iterator I = Vec.begin();
220
1.39k
      if (D->getIdentifierNamespace() != Decl::IDNS_Using) {
221
87
        while (I != Vec.end() &&
222
87
               
(*I)->getIdentifierNamespace() == Decl::IDNS_Using45
)
223
1
          ++I;
224
86
      }
225
1.39k
      Vec.insert(I, D);
226
1.39k
227
1.39k
    // All other declarations go at the end of the list, but before any
228
1.39k
    // tag declarations.  But we can be clever about tag declarations
229
1.39k
    // because there can only ever be one in a scope.
230
1.96M
    } else if (!Vec.empty() && 
Vec.back()->hasTagIdentifierNamespace()1.95M
) {
231
6.14k
      NamedDecl *TagD = Vec.back();
232
6.14k
      Vec.back() = D;
233
6.14k
      Vec.push_back(TagD);
234
6.14k
    } else
235
1.95M
      Vec.push_back(D);
236
1.96M
  }
237
};
238
239
class StoredDeclsMap
240
    : public llvm::SmallDenseMap<DeclarationName, StoredDeclsList, 4> {
241
public:
242
  static void DestroyAll(StoredDeclsMap *Map, bool Dependent);
243
244
private:
245
  friend class ASTContext; // walks the chain deleting these
246
  friend class DeclContext;
247
248
  llvm::PointerIntPair<StoredDeclsMap*, 1> Previous;
249
};
250
251
class DependentStoredDeclsMap : public StoredDeclsMap {
252
public:
253
411k
  DependentStoredDeclsMap() = default;
254
255
private:
256
  friend class DeclContext; // iterates over diagnostics
257
  friend class DependentDiagnostic;
258
259
  DependentDiagnostic *FirstDiagnostic = nullptr;
260
};
261
262
} // namespace clang
263
264
#endif // LLVM_CLANG_AST_DECLCONTEXTINTERNALS_H