Coverage Report

Created: 2020-02-25 14:32

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Frontend/InterfaceStubFunctionsConsumer.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- InterfaceStubFunctionsConsumer.cpp -------------------------------===//
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
#include "clang/AST/Mangle.h"
10
#include "clang/AST/RecursiveASTVisitor.h"
11
#include "clang/Frontend/CompilerInstance.h"
12
#include "clang/Frontend/FrontendActions.h"
13
#include "clang/Sema/TemplateInstCallback.h"
14
#include "llvm/BinaryFormat/ELF.h"
15
16
using namespace clang;
17
18
namespace {
19
class InterfaceStubFunctionsConsumer : public ASTConsumer {
20
  CompilerInstance &Instance;
21
  StringRef InFile;
22
  StringRef Format;
23
  std::set<std::string> ParsedTemplates;
24
25
  enum RootDeclOrigin { TopLevel = 0, FromTU = 1, IsLate = 2 };
26
  struct MangledSymbol {
27
    std::string ParentName;
28
    uint8_t Type;
29
    uint8_t Binding;
30
    std::vector<std::string> Names;
31
    MangledSymbol() = delete;
32
33
    MangledSymbol(const std::string &ParentName, uint8_t Type, uint8_t Binding,
34
                  std::vector<std::string> Names)
35
109
        : ParentName(ParentName), Type(Type), Binding(Binding), Names(Names) {}
36
  };
37
  using MangledSymbols = std::map<const NamedDecl *, MangledSymbol>;
38
39
336
  bool WriteNamedDecl(const NamedDecl *ND, MangledSymbols &Symbols, int RDO) {
40
336
    // Here we filter out anything that's not set to DefaultVisibility.
41
336
    // DefaultVisibility is set on a decl when -fvisibility is not specified on
42
336
    // the command line (or specified as default) and the decl does not have
43
336
    // __attribute__((visibility("hidden"))) set or when the command line
44
336
    // argument is set to hidden but the decl explicitly has
45
336
    // __attribute__((visibility ("default"))) set. We do this so that the user
46
336
    // can have fine grain control of what they want to expose in the stub.
47
432
    auto isVisible = [](const NamedDecl *ND) -> bool {
48
432
      return ND->getVisibility() == DefaultVisibility;
49
432
    };
50
336
51
336
    auto ignoreDecl = [this, isVisible](const NamedDecl *ND) -> bool {
52
313
      if (!isVisible(ND))
53
73
        return true;
54
240
55
240
      if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) {
56
40
        if (const auto *Parent = VD->getParentFunctionOrMethod())
57
18
          if (isa<BlockDecl>(Parent) || 
isa<CXXMethodDecl>(Parent)17
)
58
2
            return true;
59
38
60
38
        if ((VD->getStorageClass() == StorageClass::SC_Extern) ||
61
38
            
(36
VD->getStorageClass() == StorageClass::SC_Static36
&&
62
36
             
VD->getParentFunctionOrMethod() == nullptr13
))
63
5
          return true;
64
233
      }
65
233
66
233
      if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
67
200
        if (FD->isInlined() && 
!isa<CXXMethodDecl>(FD)119
&&
68
200
            
!Instance.getLangOpts().GNUInline16
)
69
4
          return true;
70
196
        if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
71
119
          if (const auto *RC = dyn_cast<CXXRecordDecl>(MD->getParent()))
72
119
            if (isa<ClassTemplateDecl>(RC->getParent()) || !isVisible(RC))
73
0
              return true;
74
119
          if (MD->isDependentContext() || 
!MD->hasBody()101
)
75
94
            return true;
76
102
        }
77
102
        if (FD->getStorageClass() == StorageClass::SC_Static)
78
5
          return true;
79
130
      }
80
130
      return false;
81
130
    };
82
336
83
336
    auto getParentFunctionDecl = [](const NamedDecl *ND) -> const NamedDecl * {
84
296
      if (const VarDecl *VD = dyn_cast<VarDecl>(ND))
85
44
        if (const auto *FD =
86
21
                dyn_cast_or_null<FunctionDecl>(VD->getParentFunctionOrMethod()))
87
21
          return FD;
88
275
      return nullptr;
89
275
    };
90
336
91
336
    auto getMangledNames = [](const NamedDecl *ND) -> std::vector<std::string> {
92
218
      if (!ND)
93
93
        return {""};
94
125
      ASTNameGenerator NameGen(ND->getASTContext());
95
125
      std::vector<std::string> MangledNames = NameGen.getAllManglings(ND);
96
125
      if (isa<CXXConstructorDecl>(ND) || 
isa<CXXDestructorDecl>(ND)115
)
97
16
        return MangledNames;
98
#ifdef EXPENSIVE_CHECKS
99
      assert(MangledNames.size() <= 1 && "Expected only one name mangling.");
100
#endif
101
109
      return {NameGen.getName(ND)};
102
109
    };
103
336
104
336
    if (!(RDO & FromTU))
105
0
      return true;
106
336
    if (Symbols.find(ND) != Symbols.end())
107
15
      return true;
108
321
    // - Currently have not figured out how to produce the names for FieldDecls.
109
321
    // - Do not want to produce symbols for function paremeters.
110
321
    if (isa<FieldDecl>(ND) || 
isa<ParmVarDecl>(ND)315
)
111
25
      return true;
112
296
113
296
    const NamedDecl *ParentDecl = getParentFunctionDecl(ND);
114
296
    if ((ParentDecl && 
ignoreDecl(ParentDecl)21
) ||
ignoreDecl(ND)292
)
115
183
      return true;
116
113
117
113
    if (RDO & IsLate) {
118
0
      Instance.getDiagnostics().Report(diag::err_asm_invalid_type_in_input)
119
0
          << "Generating Interface Stubs is not supported with "
120
0
             "delayed template parsing.";
121
113
    } else {
122
113
      if (const auto *FD = dyn_cast<FunctionDecl>(ND))
123
80
        if (FD->isDependentContext())
124
4
          return true;
125
109
126
109
      const bool IsWeak = (ND->hasAttr<WeakAttr>() ||
127
109
                           
ND->hasAttr<WeakRefAttr>()106
||
ND->isWeakImported()106
);
128
109
129
109
      Symbols.insert(std::make_pair(
130
109
          ND,
131
109
          MangledSymbol(getMangledNames(ParentDecl).front(),
132
109
                        // Type:
133
109
                        isa<VarDecl>(ND) ? 
llvm::ELF::STT_OBJECT33
134
109
                                         : 
llvm::ELF::STT_FUNC76
,
135
109
                        // Binding:
136
109
                        IsWeak ? 
llvm::ELF::STB_WEAK3
:
llvm::ELF::STB_GLOBAL106
,
137
109
                        getMangledNames(ND))));
138
109
    }
139
113
    
return true109
;
140
113
  }
141
142
  void
143
  HandleDecls(const llvm::iterator_range<DeclContext::decl_iterator> &Decls,
144
96
              MangledSymbols &Symbols, int RDO) {
145
96
    for (const auto *D : Decls)
146
222
      HandleNamedDecl(dyn_cast<NamedDecl>(D), Symbols, RDO);
147
96
  }
148
149
  void HandleTemplateSpecializations(const FunctionTemplateDecl &FTD,
150
5
                                     MangledSymbols &Symbols, int RDO) {
151
5
    for (const auto *D : FTD.specializations())
152
6
      HandleNamedDecl(dyn_cast<NamedDecl>(D), Symbols, RDO);
153
5
  }
154
155
  void HandleTemplateSpecializations(const ClassTemplateDecl &CTD,
156
18
                                     MangledSymbols &Symbols, int RDO) {
157
18
    for (const auto *D : CTD.specializations())
158
7
      HandleNamedDecl(dyn_cast<NamedDecl>(D), Symbols, RDO);
159
18
  }
160
161
525
  bool HandleNamedDecl(const NamedDecl *ND, MangledSymbols &Symbols, int RDO) {
162
525
    if (!ND)
163
6
      return false;
164
519
165
519
    switch (ND->getKind()) {
166
0
    default:
167
0
      break;
168
6
    case Decl::Kind::Namespace:
169
6
      HandleDecls(cast<NamespaceDecl>(ND)->decls(), Symbols, RDO);
170
6
      return true;
171
83
    case Decl::Kind::CXXRecord:
172
83
      HandleDecls(cast<CXXRecordDecl>(ND)->decls(), Symbols, RDO);
173
83
      return true;
174
7
    case Decl::Kind::ClassTemplateSpecialization:
175
7
      HandleDecls(cast<ClassTemplateSpecializationDecl>(ND)->decls(), Symbols,
176
7
                  RDO);
177
7
      return true;
178
18
    case Decl::Kind::ClassTemplate:
179
18
      HandleTemplateSpecializations(*cast<ClassTemplateDecl>(ND), Symbols, RDO);
180
18
      return true;
181
5
    case Decl::Kind::FunctionTemplate:
182
5
      HandleTemplateSpecializations(*cast<FunctionTemplateDecl>(ND), Symbols,
183
5
                                    RDO);
184
5
      return true;
185
57
    case Decl::Kind::Record:
186
57
    case Decl::Kind::Typedef:
187
57
    case Decl::Kind::Enum:
188
57
    case Decl::Kind::EnumConstant:
189
57
    case Decl::Kind::TemplateTypeParm:
190
57
    case Decl::Kind::NonTypeTemplateParm:
191
57
    case Decl::Kind::CXXConversion:
192
57
    case Decl::Kind::UnresolvedUsingValue:
193
57
    case Decl::Kind::Using:
194
57
    case Decl::Kind::UsingShadow:
195
57
    case Decl::Kind::TypeAliasTemplate:
196
57
    case Decl::Kind::TypeAlias:
197
57
    case Decl::Kind::VarTemplate:
198
57
    case Decl::Kind::VarTemplateSpecialization:
199
57
    case Decl::Kind::UsingDirective:
200
57
    case Decl::Kind::TemplateTemplateParm:
201
57
    case Decl::Kind::ClassTemplatePartialSpecialization:
202
57
    case Decl::Kind::IndirectField:
203
57
    case Decl::Kind::ConstructorUsingShadow:
204
57
    case Decl::Kind::CXXDeductionGuide:
205
57
    case Decl::Kind::NamespaceAlias:
206
57
    case Decl::Kind::UnresolvedUsingTypename:
207
57
      return true;
208
57
    case Decl::Kind::Var: {
209
51
      // Bail on any VarDecl that either has no named symbol.
210
51
      if (!ND->getIdentifier())
211
1
        return true;
212
50
      const auto *VD = cast<VarDecl>(ND);
213
50
      // Bail on any VarDecl that is a dependent or templated type.
214
50
      if (VD->isTemplated() || 
VD->getType()->isDependentType()44
)
215
6
        return true;
216
44
      if (WriteNamedDecl(ND, Symbols, RDO))
217
44
        return true;
218
0
      break;
219
0
    }
220
292
    case Decl::Kind::ParmVar:
221
292
    case Decl::Kind::CXXMethod:
222
292
    case Decl::Kind::CXXConstructor:
223
292
    case Decl::Kind::CXXDestructor:
224
292
    case Decl::Kind::Function:
225
292
    case Decl::Kind::Field:
226
292
      if (WriteNamedDecl(ND, Symbols, RDO))
227
292
        return true;
228
0
    }
229
0
230
0
    // While interface stubs are in the development stage, it's probably best to
231
0
    // catch anything that's not a VarDecl or Template/FunctionDecl.
232
0
    Instance.getDiagnostics().Report(diag::err_asm_invalid_type_in_input)
233
0
        << "Expected a function or function template decl.";
234
0
    return false;
235
0
  }
236
237
public:
238
  InterfaceStubFunctionsConsumer(CompilerInstance &Instance, StringRef InFile,
239
                                 StringRef Format)
240
65
      : Instance(Instance), InFile(InFile), Format(Format) {}
241
242
65
  void HandleTranslationUnit(ASTContext &context) override {
243
65
    struct Visitor : public RecursiveASTVisitor<Visitor> {
244
290
      bool VisitNamedDecl(NamedDecl *ND) {
245
290
        if (const auto *FD = dyn_cast<FunctionDecl>(ND))
246
110
          if (FD->isLateTemplateParsed()) {
247
0
            LateParsedDecls.insert(FD);
248
0
            return true;
249
0
          }
250
290
251
290
        if (const auto *VD = dyn_cast<ValueDecl>(ND)) {
252
188
          ValueDecls.insert(VD);
253
188
          return true;
254
188
        }
255
102
256
102
        NamedDecls.insert(ND);
257
102
        return true;
258
102
      }
259
65
260
65
      std::set<const NamedDecl *> LateParsedDecls;
261
65
      std::set<NamedDecl *> NamedDecls;
262
65
      std::set<const ValueDecl *> ValueDecls;
263
65
    } v;
264
65
265
65
    v.TraverseDecl(context.getTranslationUnitDecl());
266
65
267
65
    MangledSymbols Symbols;
268
65
    auto OS = Instance.createDefaultOutputFile(/*Binary=*/false, InFile, "ifs");
269
65
    if (!OS)
270
0
      return;
271
65
272
65
    if (Instance.getLangOpts().DelayedTemplateParsing) {
273
1
      clang::Sema &S = Instance.getSema();
274
1
      for (const auto *FD : v.LateParsedDecls) {
275
0
        clang::LateParsedTemplate &LPT =
276
0
            *S.LateParsedTemplateMap.find(cast<FunctionDecl>(FD))->second;
277
0
        S.LateTemplateParser(S.OpaqueParser, LPT);
278
0
        HandleNamedDecl(FD, Symbols, (FromTU | IsLate));
279
0
      }
280
1
    }
281
65
282
65
    for (const NamedDecl *ND : v.ValueDecls)
283
188
      HandleNamedDecl(ND, Symbols, FromTU);
284
65
    for (const NamedDecl *ND : v.NamedDecls)
285
102
      HandleNamedDecl(ND, Symbols, FromTU);
286
65
287
65
    auto writeIfsV1 = [this](const llvm::Triple &T,
288
65
                             const MangledSymbols &Symbols,
289
65
                             const ASTContext &context, StringRef Format,
290
65
                             raw_ostream &OS) -> void {
291
65
      OS << "--- !" << Format << "\n";
292
65
      OS << "IfsVersion: 1.0\n";
293
65
      OS << "Triple: " << T.str() << "\n";
294
65
      OS << "ObjectFileFormat: "
295
65
         << "ELF"
296
65
         << "\n"; // TODO: For now, just ELF.
297
65
      OS << "Symbols:\n";
298
109
      for (const auto &E : Symbols) {
299
109
        const MangledSymbol &Symbol = E.second;
300
131
        for (auto Name : Symbol.Names) {
301
131
          OS << "  \""
302
131
             << (Symbol.ParentName.empty() || 
Instance.getLangOpts().CPlusPlus16
303
131
                     ? 
""121
304
131
                     : 
(Symbol.ParentName + ".")10
)
305
131
             << Name << "\" : { Type: ";
306
131
          switch (Symbol.Type) {
307
0
          default:
308
0
            llvm_unreachable(
309
0
                "clang -emit-interface-stubs: Unexpected symbol type.");
310
0
          case llvm::ELF::STT_NOTYPE:
311
0
            OS << "NoType";
312
0
            break;
313
33
          case llvm::ELF::STT_OBJECT: {
314
33
            auto VD = cast<ValueDecl>(E.first)->getType();
315
33
            OS << "Object, Size: "
316
33
               << context.getTypeSizeInChars(VD).getQuantity();
317
33
            break;
318
0
          }
319
98
          case llvm::ELF::STT_FUNC:
320
98
            OS << "Func";
321
98
            break;
322
131
          }
323
131
          if (Symbol.Binding == llvm::ELF::STB_WEAK)
324
3
            OS << ", Weak: true";
325
131
          OS << " }\n";
326
131
        }
327
109
      }
328
65
      OS << "...\n";
329
65
      OS.flush();
330
65
    };
331
65
332
65
    assert(Format == "experimental-ifs-v1" && "Unexpected IFS Format.");
333
65
    writeIfsV1(Instance.getTarget().getTriple(), Symbols, context, Format, *OS);
334
65
  }
335
};
336
} // namespace
337
338
std::unique_ptr<ASTConsumer>
339
GenerateInterfaceIfsExpV1Action::CreateASTConsumer(CompilerInstance &CI,
340
65
                                                   StringRef InFile) {
341
65
  return std::make_unique<InterfaceStubFunctionsConsumer>(
342
65
      CI, InFile, "experimental-ifs-v1");
343
65
}