dang created this revision.
dang added reviewers: zixuw, ributzka, QuietMisdreavus.
Herald added a project: All.
dang requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Adds `--product-name=` flag to the clang driver. This gets forwarded to
cc1 only when we are performing a ExtractAPI Action. This is used to
populate the `name` field of the module object in the generated SymbolGraph.

Depends on D121936 <https://reviews.llvm.org/D121936>


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D122141

Files:
  clang/include/clang/Driver/Options.td
  clang/include/clang/Frontend/FrontendOptions.h
  clang/include/clang/SymbolGraph/Serialization.h
  clang/lib/Driver/ToolChains/Clang.cpp
  clang/lib/SymbolGraph/ExtractAPIConsumer.cpp
  clang/lib/SymbolGraph/Serialization.cpp
  clang/test/SymbolGraph/global_record.c

Index: clang/test/SymbolGraph/global_record.c
===================================================================
--- clang/test/SymbolGraph/global_record.c
+++ clang/test/SymbolGraph/global_record.c
@@ -2,7 +2,7 @@
 // RUN: split-file %s %t
 // RUN: sed -e "s@INPUT_DIR@%/t@g" %t/reference.output.json.in >> \
 // RUN: %t/reference.output.json
-// RUN: %clang -extract-api -target arm64-apple-macosx \
+// RUN: %clang -extract-api --product-name=GlobalRecord -target arm64-apple-macosx \
 // RUN: %t/input.h -o %t/output.json | FileCheck -allow-empty %s
 
 // Generator version is not consistent across test runs, normalize it.
@@ -37,7 +37,7 @@
     "generator": "?"
   },
   "module": {
-    "name": "",
+    "name": "GlobalRecord",
     "platform": {
       "architecture": "arm64",
       "operatingSystem": {
Index: clang/lib/SymbolGraph/Serialization.cpp
===================================================================
--- clang/lib/SymbolGraph/Serialization.cpp
+++ clang/lib/SymbolGraph/Serialization.cpp
@@ -262,8 +262,7 @@
 
 Object Serializer::serializeModule() const {
   Object Module;
-  // FIXME: What to put in here?
-  Module["name"] = "";
+  Module["name"] = ProductName;
   serializeObject(Module, "platform", serializePlatform(API.getTarget()));
   return Module;
 }
Index: clang/lib/SymbolGraph/ExtractAPIConsumer.cpp
===================================================================
--- clang/lib/SymbolGraph/ExtractAPIConsumer.cpp
+++ clang/lib/SymbolGraph/ExtractAPIConsumer.cpp
@@ -170,17 +170,19 @@
 
 class ExtractAPIConsumer : public ASTConsumer {
 public:
-  ExtractAPIConsumer(ASTContext &Context, std::unique_ptr<raw_pwrite_stream> OS)
-      : Visitor(Context), OS(std::move(OS)) {}
+  ExtractAPIConsumer(ASTContext &Context, StringRef ProductName,
+                     std::unique_ptr<raw_pwrite_stream> OS)
+      : Visitor(Context), ProductName(ProductName), OS(std::move(OS)) {}
 
   void HandleTranslationUnit(ASTContext &Context) override {
     Visitor.TraverseDecl(Context.getTranslationUnitDecl());
-    Serializer Serializer(Visitor.getAPI());
+    Serializer Serializer(Visitor.getAPI(), ProductName);
     Serializer.serialize(*OS);
   }
 
 private:
   ExtractAPIVisitor Visitor;
+  std::string ProductName;
   std::unique_ptr<raw_pwrite_stream> OS;
 };
 } // namespace
@@ -190,8 +192,9 @@
   std::unique_ptr<raw_pwrite_stream> OS = CreateOutputFile(CI, InFile);
   if (!OS)
     return nullptr;
-  return std::make_unique<ExtractAPIConsumer>(CI.getASTContext(),
-                                              std::move(OS));
+  return std::make_unique<ExtractAPIConsumer>(
+      CI.getASTContext(), CI.getInvocation().getFrontendOpts().ProductName,
+      std::move(OS));
 }
 
 std::unique_ptr<raw_pwrite_stream>
Index: clang/lib/Driver/ToolChains/Clang.cpp
===================================================================
--- clang/lib/Driver/ToolChains/Clang.cpp
+++ clang/lib/Driver/ToolChains/Clang.cpp
@@ -4637,6 +4637,8 @@
     assert(JA.getType() == types::TY_API_INFO &&
            "Extract API actions must generate a API information.");
     CmdArgs.push_back("-extract-api");
+    if (Arg *ProductNameArg = Args.getLastArg(options::OPT_product_name_EQ))
+      ProductNameArg->render(Args, CmdArgs);
   } else {
     assert((isa<CompileJobAction>(JA) || isa<BackendJobAction>(JA)) &&
            "Invalid action for clang tool.");
Index: clang/include/clang/SymbolGraph/Serialization.h
===================================================================
--- clang/include/clang/SymbolGraph/Serialization.h
+++ clang/include/clang/SymbolGraph/Serialization.h
@@ -30,8 +30,9 @@
 
 class Serializer {
 public:
-  Serializer(const APISet &API, SerializerOption Options = {})
-      : API(API), Options(Options) {}
+  Serializer(const APISet &API, StringRef ProductName,
+             SerializerOption Options = {})
+      : API(API), ProductName(ProductName), Options(Options) {}
 
   Object serialize();
   void serialize(raw_ostream &os);
@@ -45,6 +46,7 @@
   bool shouldSkip(const APIRecord &Record) const;
 
   const APISet &API;
+  StringRef ProductName;
   SerializerOption Options;
   Array Symbols;
   Array Relationships;
Index: clang/include/clang/Frontend/FrontendOptions.h
===================================================================
--- clang/include/clang/Frontend/FrontendOptions.h
+++ clang/include/clang/Frontend/FrontendOptions.h
@@ -410,6 +410,10 @@
   /// The name of the action to run when using a plugin action.
   std::string ActionName;
 
+  // Currently this is only used as part of the `-extract-api` action.
+  /// The name of the product the input files belong too.
+  std::string ProductName;
+
   /// Args to pass to the plugins
   std::map<std::string, std::vector<std::string>> PluginArgs;
 
Index: clang/include/clang/Driver/Options.td
===================================================================
--- clang/include/clang/Driver/Options.td
+++ clang/include/clang/Driver/Options.td
@@ -1091,6 +1091,8 @@
 def exported__symbols__list : Separate<["-"], "exported_symbols_list">;
 def extract_api : Flag<["-"], "extract-api">, Flags<[CC1Option]>, Group<Action_Group>,
   HelpText<"Extract API information">;
+def product_name_EQ: Joined<["--"], "product-name=">, Flags<[CC1Option]>,
+  MarshallingInfoString<FrontendOpts<"ProductName">>;
 def e : JoinedOrSeparate<["-"], "e">, Flags<[LinkerInput]>, Group<Link_Group>;
 def fmax_tokens_EQ : Joined<["-"], "fmax-tokens=">, Group<f_Group>, Flags<[CC1Option]>,
   HelpText<"Max total number of preprocessed tokens for -Wmax-tokens.">,
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to