plotfi updated this revision to Diff 200393.
plotfi added a comment.

adding a nice inline test


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D60974/new/

https://reviews.llvm.org/D60974

Files:
  clang/include/clang/Driver/Options.td
  clang/include/clang/Driver/Types.def
  clang/include/clang/Frontend/FrontendActions.h
  clang/include/clang/Frontend/FrontendOptions.h
  clang/lib/Driver/Driver.cpp
  clang/lib/Driver/ToolChains/Clang.cpp
  clang/lib/Frontend/CMakeLists.txt
  clang/lib/Frontend/CompilerInvocation.cpp
  clang/lib/Frontend/InterfaceStubFunctionsConsumer.cpp
  clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp
  clang/test/InterfaceStubs/bad-format.cpp
  clang/test/InterfaceStubs/class-template-specialization.cpp
  clang/test/InterfaceStubs/function-template-specialization.cpp
  clang/test/InterfaceStubs/inline.c
  clang/test/InterfaceStubs/inline.cpp
  clang/test/InterfaceStubs/inline.h
  clang/test/InterfaceStubs/object.cpp
  clang/test/InterfaceStubs/template-namespace-function.cpp
  clang/test/InterfaceStubs/visibility.cpp
  clang/test/InterfaceStubs/weak.cpp

Index: clang/test/InterfaceStubs/weak.cpp
===================================================================
--- /dev/null
+++ clang/test/InterfaceStubs/weak.cpp
@@ -0,0 +1,23 @@
+// RUN: %clang -target x86_64-linux-gnu -o - -emit-interface-stubs \
+// RUN: -interface-stub-version=experimental-tapi-elf-v1 %s | \
+// RUN: FileCheck %s
+
+// RUN: %clang -target x86_64-linux-gnu -o - -emit-interface-stubs \
+// RUN: -interface-stub-version=experimental-yaml-elf-v1 %s | \
+// RUN: FileCheck --check-prefix=CHECK-YAML %s
+
+// CHECK: Symbols:
+// CHECK-NEXT:  _Z8weakFuncv: { Type: Func, Weak: true }
+// CHECK-NEXT:  _Z10strongFuncv: { Type: Func }
+
+// CHECK-YAML: Symbols:
+// CHECK-YAML-NEXT:   - Name:            _Z8weakFuncv
+// CHECK-YAML-NEXT:     Type:            STT_FUNC
+// CHECK-YAML-NEXT:     Binding:         STB_WEAK
+// CHECK-YAML-NEXT:   - Name:            _Z10strongFuncv
+// CHECK-YAML-NEXT:     Type:            STT_FUNC
+// CHECK-YAML-NEXT:     Binding:         STB_GLOBAL
+
+__attribute__((weak)) void weakFunc() {}
+int strongFunc() {}
+
Index: clang/test/InterfaceStubs/visibility.cpp
===================================================================
--- /dev/null
+++ clang/test/InterfaceStubs/visibility.cpp
@@ -0,0 +1,29 @@
+// RUN: %clang -target x86_64-linux-gnu -o - -emit-interface-stubs \
+// RUN: -interface-stub-version=experimental-tapi-elf-v1 -fvisibility=hidden \
+// RUN: %s | FileCheck --check-prefix=CHECK-CMD-HIDDEN %s
+
+// RUN: %clang -target x86_64-linux-gnu -o - -emit-interface-stubs \
+// RUN: -interface-stub-version=experimental-yaml-elf-v1 -fvisibility=hidden \
+// RUN: %s | FileCheck --check-prefix=CHECK-CMD-HIDDEN %s
+
+// RUN: %clang -target x86_64-linux-gnu -o - -emit-interface-stubs \
+// RUN: -interface-stub-version=experimental-tapi-elf-v1 %s | FileCheck %s
+
+// RUN: %clang -target x86_64-linux-gnu -o - -emit-interface-stubs \
+// RUN: -interface-stub-version=experimental-yaml-elf-v1 %s | FileCheck %s
+
+// Always Be Hidden:
+// CHECK-CMD-HIDDEN-NOT: _Z6hiddenv
+// CHECK-NOT: _Z6hiddenv
+__attribute__((visibility("hidden"))) void hidden() {}
+
+// Always Be Visible:
+// CHECK-CMD-HIDDEN: _Z9nothiddenv
+// CHECK: _Z9nothiddenv
+__attribute__((visibility("default"))) void nothidden() {}
+
+// Do Whatever -fvisibility says:
+// CHECK-CMD-HIDDEN-NOT: _Z10cmdVisiblev
+// CHECK: _Z10cmdVisiblev
+void cmdVisible() {}
+
Index: clang/test/InterfaceStubs/template-namespace-function.cpp
===================================================================
--- /dev/null
+++ clang/test/InterfaceStubs/template-namespace-function.cpp
@@ -0,0 +1,23 @@
+// RUN: %clang -target x86_64-linux-gnu -o - -emit-interface-stubs \
+// RUN: -interface-stub-version=experimental-tapi-elf-v1 %s | \
+// RUN: FileCheck %s
+
+// CHECK: Symbols:
+// CHECK-NEXT:  _ZN3qux3barEii: { Type: Func }
+// CHECK-NEXT:  _ZN3baz3addIiEET_S1_S1_: { Type: Func }
+// CHECK-NEXT:  _Z4fbarff: { Type: Func }
+// CHECK-NEXT:  _ZN3baz3addIfEET_S1_S1_: { Type: Func }
+
+namespace baz {
+template <typename T>
+T add(T a, T b) {
+  return a + b;
+}
+} // namespace baz
+
+namespace qux {
+int bar(int a, int b) { return baz::add<int>(a, b); }
+} // namespace qux
+
+float fbar(float a, float b) { return baz::add<float>(a, b); }
+
Index: clang/test/InterfaceStubs/object.cpp
===================================================================
--- /dev/null
+++ clang/test/InterfaceStubs/object.cpp
@@ -0,0 +1,7 @@
+// RUN: %clang -target x86_64-linux-gnu -o - -emit-interface-stubs \
+// RUN: -interface-stub-version=experimental-tapi-elf-v1 %s | \
+// RUN: FileCheck %s
+
+// CHECK: data: { Type: Object, Size: 4 }
+int data = 1844;
+
Index: clang/test/InterfaceStubs/inline.h
===================================================================
--- /dev/null
+++ clang/test/InterfaceStubs/inline.h
@@ -0,0 +1,6 @@
+
+inline int fvih() {
+static int fortytwo = 42;
+  return fortytwo;
+}
+
Index: clang/test/InterfaceStubs/inline.cpp
===================================================================
--- /dev/null
+++ clang/test/InterfaceStubs/inline.cpp
@@ -0,0 +1,9 @@
+// RUN: %clang -target x86_64-linux-gnu -o - -emit-interface-stubs \
+// RUN: -interface-stub-version=experimental-tapi-elf-v1 %s | \
+// RUN: FileCheck %s
+
+// CHECK: _Z4fvihv: { Type: Func }
+// CHECK: _ZZ4fvihvE8fortytwo: { Type: Object, Size: 4 }
+
+#include "inline.h"
+
Index: clang/test/InterfaceStubs/inline.c
===================================================================
--- /dev/null
+++ clang/test/InterfaceStubs/inline.c
@@ -0,0 +1,28 @@
+// RUN: %clang -DINLINE=inline -target x86_64-unknown-linux-gnu -o - \
+// RUN: -emit-interface-stubs \
+// RUN: -interface-stub-version=experimental-yaml-elf-v1 -std=gnu89 -xc %s | \
+// RUN: FileCheck -check-prefix=CHECK-GNU %s
+// RUN: %clang -DINLINE=inline -target x86_64-linux-gnu -O0 -o - -c \
+// RUN: -std=gnu89 -xc %s | llvm-nm - | FileCheck -check-prefix=CHECK-GNU %s
+
+// RUN: %clang -DINLINE="__attribute__((always_inline))" \
+// RUN: -target x86_64-unknown-linux-gnu -o - -emit-interface-stubs \
+// RUN: -interface-stub-version=experimental-yaml-elf-v1 -xc %s | \
+// RUN: FileCheck -check-prefix=CHECK-GNU %s
+// RUN: %clang -DINLINE="__attribute__((always_inline))" \
+// RUN: -target x86_64-linux-gnu -O0 -o - -c -xc %s | \
+// RUN: llvm-nm - | FileCheck -check-prefix=CHECK-GNU %s
+
+// RUN: %clang -DINLINE=inline -target x86_64-unknown-linux-gnu -o - \
+// RUN: -emit-interface-stubs \
+// RUN: -interface-stub-version=experimental-yaml-elf-v1 -std=c99 -xc %s | \
+// RUN: FileCheck -check-prefix=CHECK-STD %s
+// RUN: %clang -DINLINE=inline -target x86_64-linux-gnu -O0 -o - -c -std=c99 \
+// RUN: -xc %s | llvm-nm - 2>&1 | FileCheck -check-prefix=CHECK-STD %s
+
+// CHECK-GNU: foo
+// CHECK-GNU: foo.var
+// CHECK-STD-NOT: foo
+#pragma clang diagnostic ignored "-Wstatic-local-in-inline"
+INLINE void foo() { static int var = 1234; }
+
Index: clang/test/InterfaceStubs/function-template-specialization.cpp
===================================================================
--- /dev/null
+++ clang/test/InterfaceStubs/function-template-specialization.cpp
@@ -0,0 +1,26 @@
+// RUN: %clang -target x86_64-linux-gnu -o - -emit-interface-stubs \
+// RUN: -interface-stub-version=experimental-tapi-elf-v1 %s | FileCheck %s
+
+// RUN: %clang -target x86_64-linux-gnu -o - -emit-interface-stubs \
+// RUN: -interface-stub-version=experimental-tapi-elf-v1 \
+// RUN: -DUSE_TEMPLATE_FUNCTION=1 %s | \
+// RUN: FileCheck --check-prefix=CHECK-USES-TEMPLATE-FUNCTION %s
+
+// RUN: %clang -target x86_64-linux-gnu -o - -emit-interface-stubs \
+// RUN: -interface-stub-version=experimental-tapi-elf-v1 \
+// RUN: -DSPECIALIZE_TEMPLATE_FUNCTION=1 %s | \
+// RUN: FileCheck --check-prefix=CHECK-SPECIALIZES-TEMPLATE-FUNCTION %s
+
+// CHECK-NOT: _Z16templateFunctionIiET_S0_
+// CHECK-USES-TEMPLATE-FUNCTION: _Z16templateFunctionIiET_S0_
+// CHECK-SPECIALIZES-TEMPLATE-FUNCTION: _Z16templateFunctionIiET_S0_
+template <typename T> T templateFunction(T t) { return t; }
+
+#ifdef USE_TEMPLATE_FUNCTION
+int FortyTwo = templateFunction<int>(42);
+#endif
+
+#ifdef SPECIALIZE_TEMPLATE_FUNCTION
+template<> int templateFunction<int>(int t);
+#endif
+
Index: clang/test/InterfaceStubs/class-template-specialization.cpp
===================================================================
--- /dev/null
+++ clang/test/InterfaceStubs/class-template-specialization.cpp
@@ -0,0 +1,31 @@
+// RUN: %clang -target x86_64-linux-gnu -o - -emit-interface-stubs \
+// RUN: -interface-stub-version=experimental-tapi-elf-v1 %s | \
+// RUN: FileCheck %s
+
+// For the following:
+// g()
+// n::S<int>::S()
+// n::S<int>::~S()
+// n::S<int>::func() const
+// n::S<int>::S(n::S<int> const&)
+
+// We expect these manglings:
+// CHECK: Symbols:
+// CHECK-NEXT:  _Z1gv: { Type: Func }
+// CHECK-NEXT:  _ZN1n1SIiEC1Ev: { Type: Func }
+// CHECK-NEXT:  _ZN1n1SIiED1Ev: { Type: Func }
+// CHECK-NEXT:  _ZNK1n1SIiE4funcEv: { Type: Func }
+// CHECK-NEXT:  _ZN1n1SIiEC1ERKS1_: { Type: Func }
+
+namespace n {
+template <typename T>
+struct __attribute__((__visibility__("default"))) S {
+  S() = default;
+  ~S() = default;
+  int __attribute__((__visibility__(("default")))) func() const { return 32; }
+  int __attribute__((__visibility__(("hidden")))) operator()() const { return 53; }
+};
+} // namespace n
+
+void g() { n::S<int>()(); }
+
Index: clang/test/InterfaceStubs/bad-format.cpp
===================================================================
--- /dev/null
+++ clang/test/InterfaceStubs/bad-format.cpp
@@ -0,0 +1,14 @@
+// RUN: %clang -target x86_64-linux-gnu -o - -emit-interface-stubs \
+// RUN: -interface-stub-version=bar-format %s 2>&1 | filecheck %
+
+// XFAIL: *
+
+// CHECK: error: invalid value \
+// CHECK: '-interface-stub-version=<experimental-tapi-elf-v0 \
+// CHECK: | experimental-yaml-elf-v1>' in 'Must specify a valid interface \
+// CHECK: stub format type using '
+
+int foo() {
+  return 42;
+}
+
Index: clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp
===================================================================
--- clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp
+++ clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp
@@ -64,6 +64,10 @@
   case GenerateHeaderModule:
     return llvm::make_unique<GenerateHeaderModuleAction>();
   case GeneratePCH:            return llvm::make_unique<GeneratePCHAction>();
+  case GenerateInterfaceYAMLExpV1:
+    return llvm::make_unique<GenerateInterfaceYAMLExpV1Action>();
+  case GenerateInterfaceTBEExpV1:
+    return llvm::make_unique<GenerateInterfaceTBEExpV1Action>();
   case InitOnly:               return llvm::make_unique<InitOnlyAction>();
   case ParseSyntaxOnly:        return llvm::make_unique<SyntaxOnlyAction>();
   case ModuleFileInfo:         return llvm::make_unique<DumpModuleInfoAction>();
Index: clang/lib/Frontend/InterfaceStubFunctionsConsumer.cpp
===================================================================
--- /dev/null
+++ clang/lib/Frontend/InterfaceStubFunctionsConsumer.cpp
@@ -0,0 +1,325 @@
+//===--- InterfaceStubFunctionsConsumer.cpp -------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang/AST/RecursiveASTVisitor.h"
+#include "clang/Frontend/CompilerInstance.h"
+#include "clang/Frontend/FrontendActions.h"
+#include "clang/Index/CodegenNameGenerator.h"
+#include "clang/Sema/TemplateInstCallback.h"
+#include "llvm/BinaryFormat/ELF.h"
+
+using namespace clang;
+
+class InterfaceStubFunctionsConsumer : public ASTConsumer {
+  CompilerInstance &Instance;
+  StringRef InFile = "";
+  StringRef Format = "";
+  std::set<std::string> ParsedTemplates;
+
+  enum RootDeclOrigin { TopLevel = 0, FromTU = 1, IsLate = 2 };
+  struct MangledSymbol {
+    std::string Name = "";
+    std::string ParentName = "";
+    uint8_t Type;
+    uint8_t Binding;
+    MangledSymbol() = delete;
+    MangledSymbol(const std::string &Name, const std::string &ParentName,
+                  uint8_t Type, uint8_t Binding)
+        : Name(Name), ParentName(ParentName), Type(Type), Binding(Binding) {}
+  };
+  using MangledSymbols = std::map<const NamedDecl *, MangledSymbol>;
+
+  bool WriteNamedDecl(const NamedDecl *ND, MangledSymbols &Symbols, int RDO) {
+    if (!(RDO & FromTU))
+      return true;
+    if (Symbols.find(ND) != Symbols.end())
+      return true;
+    if (isa<ParmVarDecl>(ND))
+      return true;
+
+    // Here we filter out anything that's not set to DefaultVisibility.
+    // DefaultVisibility is set on a decl when -fvisibility is not specified on
+    // the command line (or specified as default) and the decl does not have
+    // __attribute__((visibility("hidden"))) set or when the command line
+    // argument is set to hidden but the decl explicitly has
+    // __attribute__((visibility ("default"))) set. We do this so that the user
+    // can have fine grain control of what they want to expose in the stub.
+    if (ND->getVisibility() != DefaultVisibility)
+      return true;
+
+    if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
+      if (FD->isInlined() && !Instance.getLangOpts().GNUInline)
+        return true;
+
+    auto getMangledName = [](const NamedDecl *ND) -> std::string {
+      if (!ND)
+        return "";
+      index::CodegenNameGenerator CGNameGen(ND->getASTContext());
+      return CGNameGen.getName(ND);
+    };
+
+    const NamedDecl *ParentDecl = nullptr;
+    if (const VarDecl *VD = dyn_cast<VarDecl>(ND))
+      if (const auto *FD =
+              dyn_cast_or_null<FunctionDecl>(VD->getParentFunctionOrMethod())) {
+        if (FD->isInlined() && !Instance.getLangOpts().GNUInline)
+          return true;
+        ParentDecl = FD;
+      }
+
+    bool IsRDOLate = (RDO & IsLate);
+    if (const auto *FD = dyn_cast<FunctionDecl>(ND))
+      if (FD->isDependentContext() && !IsRDOLate)
+        return true;
+
+    const bool IsWeak = (ND->hasAttr<WeakAttr>() ||
+                         ND->hasAttr<WeakRefAttr>() || ND->isWeakImported());
+    uint8_t Type =
+        isa<VarDecl>(ND) ? llvm::ELF::STT_OBJECT : llvm::ELF::STT_FUNC;
+    uint8_t Binding = IsWeak ? llvm::ELF::STB_WEAK : llvm::ELF::STB_GLOBAL;
+    Symbols.insert(std::make_pair(ND, MangledSymbol(getMangledName(ND),
+                                                    getMangledName(ParentDecl),
+                                                    Type, Binding)));
+
+    if (IsRDOLate)
+      llvm_unreachable("Generating Interface Stubs is not supported with "
+                       "delayed template parsing.");
+    return true;
+  }
+
+  void
+  HandleDecls(const llvm::iterator_range<DeclContext::decl_iterator> &Decls,
+              MangledSymbols &Symbols, int RDO) {
+    for (const auto *D : Decls)
+      HandleNamedDecl(dyn_cast<NamedDecl>(D), Symbols, RDO);
+  }
+
+  void HandleTemplateSpecializations(const FunctionTemplateDecl &FTD,
+                                     MangledSymbols &Symbols, int RDO) {
+    for (const auto *D : FTD.specializations())
+      HandleNamedDecl(dyn_cast<NamedDecl>(D), Symbols, RDO);
+  }
+
+  void HandleTemplateSpecializations(const ClassTemplateDecl &CTD,
+                                     MangledSymbols &Symbols, int RDO) {
+    for (const auto *D : CTD.specializations())
+      HandleNamedDecl(dyn_cast<NamedDecl>(D), Symbols, RDO);
+  }
+
+  bool HandleNamedDecl(const NamedDecl *ND, MangledSymbols &Symbols, int RDO) {
+    if (!ND)
+      return false;
+
+    switch (ND->getKind()) {
+    default:
+      break;
+    case Decl::Kind::Namespace:
+      HandleDecls(cast<NamespaceDecl>(ND)->decls(), Symbols, RDO);
+      return true;
+    case Decl::Kind::CXXRecord:
+      HandleDecls(cast<CXXRecordDecl>(ND)->decls(), Symbols, RDO);
+      return true;
+    case Decl::Kind::ClassTemplateSpecialization:
+      HandleDecls(cast<ClassTemplateSpecializationDecl>(ND)->decls(), Symbols,
+                  RDO);
+      return true;
+    case Decl::Kind::ClassTemplate:
+      HandleTemplateSpecializations(*cast<ClassTemplateDecl>(ND), Symbols, RDO);
+      return true;
+    case Decl::Kind::FunctionTemplate:
+      HandleTemplateSpecializations(*cast<FunctionTemplateDecl>(ND), Symbols,
+                                    RDO);
+      return true;
+    case Decl::Kind::TemplateTypeParm:
+      return true;
+    case Decl::Kind::Var:
+    case Decl::Kind::ParmVar:
+    case Decl::Kind::CXXMethod:
+    case Decl::Kind::CXXConstructor:
+    case Decl::Kind::CXXDestructor:
+    case Decl::Kind::Function:
+      if (WriteNamedDecl(ND, Symbols, RDO))
+        return true;
+    }
+
+    // While interface stubs are in the development stage, it's probably best to
+    // catch anything that's not a VarDecl or Template/FunctionDecl.
+    llvm_unreachable("clang -emit-iterface-stubs: Expected a function or "
+                     "function template decl.");
+    return false;
+  }
+
+public:
+  InterfaceStubFunctionsConsumer(CompilerInstance &Instance, StringRef InFile,
+                                 StringRef Format)
+      : Instance(Instance), InFile(InFile), Format(Format) {}
+
+  void HandleTranslationUnit(ASTContext &context) override {
+    struct Visitor : public RecursiveASTVisitor<Visitor> {
+      bool VisitNamedDecl(NamedDecl *ND) {
+        if (const auto *FD = dyn_cast<FunctionDecl>(ND))
+          if (FD->isLateTemplateParsed()) {
+            LateParsedDecls.insert(FD);
+            return true;
+          }
+
+        if (const auto *VD = dyn_cast<ValueDecl>(ND)) {
+          ValueDecls.insert(VD);
+          return true;
+        }
+
+        NamedDecls.insert(ND);
+        return true;
+      }
+
+      std::set<const NamedDecl *> LateParsedDecls;
+      std::set<NamedDecl *> NamedDecls;
+      std::set<const ValueDecl *> ValueDecls;
+    } v;
+
+    v.TraverseDecl(context.getTranslationUnitDecl());
+
+    MangledSymbols Symbols;
+    auto OS = Instance.createDefaultOutputFile(/*Binary=*/false, InFile, "ifo");
+    if (!OS)
+      return;
+
+    if (Instance.getLangOpts().DelayedTemplateParsing) {
+      clang::Sema &S = Instance.getSema();
+      for (const auto *FD : v.LateParsedDecls) {
+        clang::LateParsedTemplate &LPT =
+            *S.LateParsedTemplateMap.find(cast<FunctionDecl>(FD))->second;
+        S.LateTemplateParser(S.OpaqueParser, LPT);
+        HandleNamedDecl(FD, Symbols, (FromTU | IsLate));
+      }
+    }
+
+    for (const NamedDecl *ND : v.ValueDecls)
+      HandleNamedDecl(ND, Symbols, FromTU);
+    for (const NamedDecl *ND : v.NamedDecls)
+      HandleNamedDecl(ND, Symbols, FromTU);
+
+    auto writeIfoYaml = [](const llvm::Triple &T, const MangledSymbols &Symbols,
+                           const ASTContext &context, StringRef Format,
+                           raw_ostream &OS) -> void {
+      OS << "--- !" << Format << "\n";
+      OS << "FileHeader:\n";
+      OS << "  Class:           ELFCLASS";
+      OS << (T.isArch64Bit() ? "64" : "32");
+      OS << "\n";
+      OS << "  Data:            ELFDATA2";
+      OS << (T.isLittleEndian() ? "LSB" : "MSB");
+      OS << "\n";
+      OS << "  Type:            ET_REL\n";
+      OS << "  Machine:         "
+         << llvm::StringSwitch<llvm::StringRef>(T.getArchName())
+                .Case("x86_64", "EM_X86_64")
+                .Case("i386", "EM_386")
+                .Case("i686", "EM_386")
+                .Case("aarch64", "EM_AARCH64")
+                .Case("amdgcn", "EM_AMDGPU")
+                .Case("r600", "EM_AMDGPU")
+                .Case("arm", "EM_ARM")
+                .Case("thumb", "EM_ARM")
+                .Case("avr", "EM_AVR")
+                .Case("mips", "EM_MIPS")
+                .Case("mipsel", "EM_MIPS")
+                .Case("mips64", "EM_MIPS")
+                .Case("mips64el", "EM_MIPS")
+                .Case("msp430", "EM_MSP430")
+                .Case("ppc", "EM_PPC")
+                .Case("ppc64", "EM_PPC64")
+                .Case("ppc64le", "EM_PPC64")
+                .Case("x86", T.isOSIAMCU() ? "EM_IAMCU" : "EM_386")
+                .Case("x86_64", "EM_X86_64")
+                .Default("EM_NONE")
+         << "\nSymbols:\n";
+      for (const auto &E : Symbols) {
+        const MangledSymbol &Symbol = E.second;
+        OS << "  - Name:            "
+           << (Symbol.ParentName.empty() ? "" : (Symbol.ParentName + "."))
+           << Symbol.Name << "\n"
+           << "    Type:            STT_";
+        switch (Symbol.Type) {
+        default:
+        case llvm::ELF::STT_NOTYPE:
+          OS << "NOTYPE";
+          break;
+        case llvm::ELF::STT_OBJECT:
+          OS << "OBJECT";
+          break;
+        case llvm::ELF::STT_FUNC:
+          OS << "FUNC";
+          break;
+        }
+        OS << "\n    Binding:         STB_"
+           << ((Symbol.Binding == llvm::ELF::STB_WEAK) ? "WEAK" : "GLOBAL")
+           << "\n";
+      }
+      OS << "...\n";
+      OS.flush();
+    };
+
+    auto writeIfoElfAbiYaml = [](const llvm::Triple &T,
+                                 const MangledSymbols &Symbols,
+                                 const ASTContext &context, StringRef Format,
+                                 raw_ostream &OS) -> void {
+      OS << "--- !" << Format << "\n";
+      OS << "TbeVersion: 1.0\n";
+      OS << "Arch: " << T.getArchName() << "\n";
+      OS << "Symbols:\n";
+      for (const auto &E : Symbols) {
+        const MangledSymbol &Symbol = E.second;
+        OS << "  " << Symbol.Name << ": { Type: ";
+        switch (Symbol.Type) {
+        default:
+          llvm_unreachable(
+              "clang -emit-iterface-stubs: Unexpected symbol type.");
+        case llvm::ELF::STT_NOTYPE:
+          OS << "NoType";
+          break;
+        case llvm::ELF::STT_OBJECT: {
+          auto VD = cast<ValueDecl>(E.first)->getType();
+          OS << "Object, Size: "
+             << context.getTypeSizeInChars(VD).getQuantity();
+          break;
+        }
+        case llvm::ELF::STT_FUNC:
+          OS << "Func";
+          break;
+        }
+        if (Symbol.Binding == llvm::ELF::STB_WEAK)
+          OS << ", Weak: true";
+        OS << " }\n";
+      }
+      OS << "...\n";
+      OS.flush();
+    };
+
+    if (Format == "experimental-yaml-elf-v1")
+      writeIfoYaml(Instance.getTarget().getTriple(), Symbols, context, Format,
+                   *OS);
+    else
+      writeIfoElfAbiYaml(Instance.getTarget().getTriple(), Symbols, context,
+                         Format, *OS);
+  }
+};
+
+std::unique_ptr<ASTConsumer>
+GenerateInterfaceYAMLExpV1Action::CreateASTConsumer(CompilerInstance &CI,
+                                                    StringRef InFile) {
+  return llvm::make_unique<InterfaceStubFunctionsConsumer>(
+      CI, InFile, "experimental-yaml-elf-v1");
+}
+
+std::unique_ptr<ASTConsumer>
+GenerateInterfaceTBEExpV1Action::CreateASTConsumer(CompilerInstance &CI,
+                                                   StringRef InFile) {
+  return llvm::make_unique<InterfaceStubFunctionsConsumer>(
+      CI, InFile, "experimental-tapi-elf-v1");
+}
Index: clang/lib/Frontend/CompilerInvocation.cpp
===================================================================
--- clang/lib/Frontend/CompilerInvocation.cpp
+++ clang/lib/Frontend/CompilerInvocation.cpp
@@ -1661,6 +1661,23 @@
       Opts.ProgramAction = frontend::GenerateHeaderModule; break;
     case OPT_emit_pch:
       Opts.ProgramAction = frontend::GeneratePCH; break;
+    case OPT_emit_iterface_stubs: {
+      auto ProgramActionPair =
+          llvm::StringSwitch<std::pair<frontend::ActionKind, bool>>(
+              Args.hasArg(OPT_iterface_stub_version_EQ)
+                  ? Args.getLastArgValue(OPT_iterface_stub_version_EQ)
+                  : "")
+              .Case("experimental-yaml-elf-v1",
+                    std::make_pair(frontend::GenerateInterfaceYAMLExpV1, true))
+              .Case("experimental-tapi-elf-v1",
+                    std::make_pair(frontend::GenerateInterfaceTBEExpV1, true))
+              .Default(
+                  std::make_pair(frontend::GenerateInterfaceTBEExpV1, false));
+      if (!ProgramActionPair.second)
+        llvm_unreachable("Must specify a valid interface stub format.");
+      Opts.ProgramAction = ProgramActionPair.first;
+      break;
+    }
     case OPT_init_only:
       Opts.ProgramAction = frontend::InitOnly; break;
     case OPT_fsyntax_only:
@@ -3086,6 +3103,8 @@
   case frontend::GenerateModuleInterface:
   case frontend::GenerateHeaderModule:
   case frontend::GeneratePCH:
+  case frontend::GenerateInterfaceYAMLExpV1:
+  case frontend::GenerateInterfaceTBEExpV1:
   case frontend::ParseSyntaxOnly:
   case frontend::ModuleFileInfo:
   case frontend::VerifyPCH:
Index: clang/lib/Frontend/CMakeLists.txt
===================================================================
--- clang/lib/Frontend/CMakeLists.txt
+++ clang/lib/Frontend/CMakeLists.txt
@@ -45,6 +45,7 @@
   TextDiagnosticBuffer.cpp
   TextDiagnosticPrinter.cpp
   VerifyDiagnosticConsumer.cpp
+  InterfaceStubFunctionsConsumer.cpp
 
   DEPENDS
   ClangDriverOptions
@@ -54,6 +55,7 @@
   clangAST
   clangBasic
   clangDriver
+  clangIndex
   clangEdit
   clangLex
   clangParse
Index: clang/lib/Driver/ToolChains/Clang.cpp
===================================================================
--- clang/lib/Driver/ToolChains/Clang.cpp
+++ clang/lib/Driver/ToolChains/Clang.cpp
@@ -3606,6 +3606,25 @@
     } else if (JA.getType() == types::TY_LLVM_BC ||
                JA.getType() == types::TY_LTO_BC) {
       CmdArgs.push_back("-emit-llvm-bc");
+    } else if (JA.getType() == types::TY_IFS) {
+      StringRef StubFormat =
+          llvm::StringSwitch<StringRef>(
+              Args.hasArg(options::OPT_iterface_stub_version_EQ)
+                  ? Args.getLastArgValue(options::OPT_iterface_stub_version_EQ)
+                  : "")
+              .Case("experimental-yaml-elf-v1", "experimental-yaml-elf-v1")
+              .Case("experimental-tapi-elf-v1", "experimental-tapi-elf-v1")
+              .Default("");
+
+      if (StubFormat.empty())
+        D.Diag(diag::err_drv_invalid_value)
+            << "Must specify a valid interface stub format type using "
+            << "-interface-stub-version=<experimental-tapi-elf-v1 | "
+               "experimental-yaml-elf-v1>";
+
+      CmdArgs.push_back("-emit-interface-stubs");
+      CmdArgs.push_back(
+          Args.MakeArgString(Twine("-interface-stub-version=") + StubFormat));
     } else if (JA.getType() == types::TY_PP_Asm) {
       CmdArgs.push_back("-S");
     } else if (JA.getType() == types::TY_AST) {
Index: clang/lib/Driver/Driver.cpp
===================================================================
--- clang/lib/Driver/Driver.cpp
+++ clang/lib/Driver/Driver.cpp
@@ -285,6 +285,7 @@
              (PhaseArg = DAL.getLastArg(options::OPT_rewrite_objc)) ||
              (PhaseArg = DAL.getLastArg(options::OPT_rewrite_legacy_objc)) ||
              (PhaseArg = DAL.getLastArg(options::OPT__migrate)) ||
+             (PhaseArg = DAL.getLastArg(options::OPT_emit_iterface_stubs)) ||
              (PhaseArg = DAL.getLastArg(options::OPT__analyze,
                                         options::OPT__analyze_auto)) ||
              (PhaseArg = DAL.getLastArg(options::OPT_emit_ast))) {
@@ -3445,6 +3446,8 @@
       return C.MakeAction<CompileJobAction>(Input, types::TY_ModuleFile);
     if (Args.hasArg(options::OPT_verify_pch))
       return C.MakeAction<VerifyPCHJobAction>(Input, types::TY_Nothing);
+    if (Args.hasArg(options::OPT_emit_iterface_stubs))
+      return C.MakeAction<CompileJobAction>(Input, types::TY_IFS);
     return C.MakeAction<CompileJobAction>(Input, types::TY_LLVM_BC);
   }
   case phases::Backend: {
Index: clang/include/clang/Frontend/FrontendOptions.h
===================================================================
--- clang/include/clang/Frontend/FrontendOptions.h
+++ clang/include/clang/Frontend/FrontendOptions.h
@@ -88,6 +88,10 @@
   /// Generate pre-compiled header.
   GeneratePCH,
 
+  /// Generate Interface Stub Files.
+  GenerateInterfaceYAMLExpV1,
+  GenerateInterfaceTBEExpV1,
+
   /// Only execute frontend initialization.
   InitOnly,
 
Index: clang/include/clang/Frontend/FrontendActions.h
===================================================================
--- clang/include/clang/Frontend/FrontendActions.h
+++ clang/include/clang/Frontend/FrontendActions.h
@@ -119,6 +119,26 @@
   bool hasASTFileSupport() const override { return false; }
 };
 
+class GenerateInterfaceStubAction : public ASTFrontendAction {
+protected:
+  TranslationUnitKind getTranslationUnitKind() override { return TU_Module; }
+
+  bool hasASTFileSupport() const override { return false; }
+};
+
+// Support different interface stub formats this way:
+class GenerateInterfaceYAMLExpV1Action : public GenerateInterfaceStubAction {
+protected:
+  std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
+                                                 StringRef InFile) override;
+};
+
+class GenerateInterfaceTBEExpV1Action : public GenerateInterfaceStubAction {
+protected:
+  std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
+                                                 StringRef InFile) override;
+};
+
 class GenerateModuleFromModuleMapAction : public GenerateModuleAction {
 private:
   bool BeginSourceFileAction(CompilerInstance &CI) override;
Index: clang/include/clang/Driver/Types.def
===================================================================
--- clang/include/clang/Driver/Types.def
+++ clang/include/clang/Driver/Types.def
@@ -88,6 +88,7 @@
 
 // Misc.
 TYPE("ast",                      AST,          INVALID,         "ast",   "u")
+TYPE("ifs",                      IFS,          INVALID,         "ifs",   "u")
 TYPE("pcm",                      ModuleFile,   INVALID,         "pcm",   "u")
 TYPE("plist",                    Plist,        INVALID,         "plist", "")
 TYPE("rewritten-objc",           RewrittenObjC,INVALID,         "cpp",   "")
Index: clang/include/clang/Driver/Options.td
===================================================================
--- clang/include/clang/Driver/Options.td
+++ clang/include/clang/Driver/Options.td
@@ -623,6 +623,9 @@
   HelpText<"Emit Clang AST files for source inputs">;
 def emit_llvm : Flag<["-"], "emit-llvm">, Flags<[CC1Option]>, Group<Action_Group>,
   HelpText<"Use the LLVM representation for assembler and object files">;
+def emit_iterface_stubs : Flag<["-"], "emit-interface-stubs">, Flags<[CC1Option]>, Group<Action_Group>,
+  HelpText<"Generate Inteface Stub Files.">;
+def iterface_stub_version_EQ : JoinedOrSeparate<["-"], "interface-stub-version=">, Flags<[CC1Option]>;
 def exported__symbols__list : Separate<["-"], "exported_symbols_list">;
 def e : JoinedOrSeparate<["-"], "e">, Group<Link_Group>;
 def fPIC : Flag<["-"], "fPIC">, Group<f_Group>;
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to