Coverage Report

Created: 2020-09-15 12:33

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/AST/ASTImporterLookupTable.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- ASTImporterLookupTable.cpp - ASTImporter specific lookup -----------===//
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 ASTImporterLookupTable class which implements a
10
//  lookup procedure for the import mechanism.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#include "clang/AST/ASTImporterLookupTable.h"
15
#include "clang/AST/Decl.h"
16
#include "clang/AST/RecursiveASTVisitor.h"
17
18
namespace clang {
19
20
namespace {
21
22
struct Builder : RecursiveASTVisitor<Builder> {
23
  ASTImporterLookupTable &LT;
24
2.14k
  Builder(ASTImporterLookupTable &LT) : LT(LT) {}
25
26
11.1k
  bool VisitTypedefNameDecl(TypedefNameDecl *D) {
27
11.1k
    QualType Ty = D->getUnderlyingType();
28
11.1k
    Ty = Ty.getCanonicalType();
29
11.1k
    if (const auto *RTy = dyn_cast<RecordType>(Ty)) {
30
2.01k
      LT.add(RTy->getAsRecordDecl());
31
      // iterate over the field decls, adding them
32
7.99k
      for (auto *it : RTy->getAsRecordDecl()->fields()) {
33
7.99k
        LT.add(it);
34
7.99k
      }
35
2.01k
    }
36
11.1k
    return true;
37
11.1k
  }
38
39
16.7k
  bool VisitNamedDecl(NamedDecl *D) {
40
16.7k
    LT.add(D);
41
16.7k
    return true;
42
16.7k
  }
43
  // In most cases the FriendDecl contains the declaration of the befriended
44
  // class as a child node, so it is discovered during the recursive
45
  // visitation. However, there are cases when the befriended class is not a
46
  // child, thus it must be fetched explicitly from the FriendDecl, and only
47
  // then can we add it to the lookup table.
48
132
  bool VisitFriendDecl(FriendDecl *D) {
49
132
    if (D->getFriendType()) {
50
76
      QualType Ty = D->getFriendType()->getType();
51
76
      if (isa<ElaboratedType>(Ty))
52
52
        Ty = cast<ElaboratedType>(Ty)->getNamedType();
53
      // A FriendDecl with a dependent type (e.g. ClassTemplateSpecialization)
54
      // always has that decl as child node.
55
      // However, there are non-dependent cases which does not have the
56
      // type as a child node. We have to dig up that type now.
57
76
      if (!Ty->isDependentType()) {
58
64
        if (const auto *RTy = dyn_cast<RecordType>(Ty))
59
44
          LT.add(RTy->getAsCXXRecordDecl());
60
20
        else if (const auto *SpecTy = dyn_cast<TemplateSpecializationType>(Ty))
61
8
          LT.add(SpecTy->getAsCXXRecordDecl());
62
12
        else if (const auto *SubstTy =
63
4
                     dyn_cast<SubstTemplateTypeParmType>(Ty)) {
64
4
          if (SubstTy->getAsCXXRecordDecl())
65
4
            LT.add(SubstTy->getAsCXXRecordDecl());
66
8
        } else if (isa<TypedefType>(Ty)) {
67
          // We do not put friend typedefs to the lookup table because
68
          // ASTImporter does not organize typedefs into redecl chains.
69
0
        } else {
70
0
          llvm_unreachable("Unhandled type of friend class");
71
0
        }
72
132
      }
73
76
    }
74
132
    return true;
75
132
  }
76
77
  // Override default settings of base.
78
404
  bool shouldVisitTemplateInstantiations() const { return true; }
79
19.7k
  bool shouldVisitImplicitCode() const { return true; }
80
};
81
82
} // anonymous namespace
83
84
2.14k
ASTImporterLookupTable::ASTImporterLookupTable(TranslationUnitDecl &TU) {
85
2.14k
  Builder B(*this);
86
2.14k
  B.TraverseDecl(&TU);
87
2.14k
}
88
89
39.3k
void ASTImporterLookupTable::add(DeclContext *DC, NamedDecl *ND) {
90
39.3k
  DeclList &Decls = LookupTable[DC][ND->getDeclName()];
91
  // Inserts if and only if there is no element in the container equal to it.
92
39.3k
  Decls.insert(ND);
93
39.3k
}
94
95
117
void ASTImporterLookupTable::remove(DeclContext *DC, NamedDecl *ND) {
96
117
  DeclList &Decls = LookupTable[DC][ND->getDeclName()];
97
117
  bool EraseResult = Decls.remove(ND);
98
117
  (void)EraseResult;
99
117
  assert(EraseResult == true && "Trying to remove not contained Decl");
100
117
}
101
102
39.2k
void ASTImporterLookupTable::add(NamedDecl *ND) {
103
39.2k
  assert(ND);
104
39.2k
  DeclContext *DC = ND->getDeclContext()->getPrimaryContext();
105
39.2k
  add(DC, ND);
106
39.2k
  DeclContext *ReDC = DC->getRedeclContext()->getPrimaryContext();
107
39.2k
  if (DC != ReDC)
108
103
    add(ReDC, ND);
109
39.2k
}
110
111
117
void ASTImporterLookupTable::remove(NamedDecl *ND) {
112
117
  assert(ND);
113
117
  DeclContext *DC = ND->getDeclContext()->getPrimaryContext();
114
117
  remove(DC, ND);
115
117
  DeclContext *ReDC = DC->getRedeclContext()->getPrimaryContext();
116
117
  if (DC != ReDC)
117
0
    remove(ReDC, ND);
118
117
}
119
120
ASTImporterLookupTable::LookupResult
121
10.5k
ASTImporterLookupTable::lookup(DeclContext *DC, DeclarationName Name) const {
122
10.5k
  auto DCI = LookupTable.find(DC->getPrimaryContext());
123
10.5k
  if (DCI == LookupTable.end())
124
1.66k
    return {};
125
8.91k
126
8.91k
  const auto &FoundNameMap = DCI->second;
127
8.91k
  auto NamesI = FoundNameMap.find(Name);
128
8.91k
  if (NamesI == FoundNameMap.end())
129
4.95k
    return {};
130
3.96k
131
3.96k
  return NamesI->second;
132
3.96k
}
133
134
0
void ASTImporterLookupTable::dump(DeclContext *DC) const {
135
0
  auto DCI = LookupTable.find(DC->getPrimaryContext());
136
0
  if (DCI == LookupTable.end())
137
0
    llvm::errs() << "empty\n";
138
0
  const auto &FoundNameMap = DCI->second;
139
0
  for (const auto &Entry : FoundNameMap) {
140
0
    DeclarationName Name = Entry.first;
141
0
    llvm::errs() << "==== Name: ";
142
0
    Name.dump();
143
0
    const DeclList& List = Entry.second;
144
0
    for (NamedDecl *ND : List) {
145
0
      ND->dump();
146
0
    }
147
0
  }
148
0
}
149
150
0
void ASTImporterLookupTable::dump() const {
151
0
  for (const auto &Entry : LookupTable) {
152
0
    DeclContext *DC = Entry.first;
153
0
    StringRef Primary = DC->getPrimaryContext() ? " primary" : "";
154
0
    llvm::errs() << "== DC:" << cast<Decl>(DC) << Primary << "\n";
155
0
    dump(DC);
156
0
  }
157
0
}
158
159
} // namespace clang