Skip to content

Commit 5ef2ec7

Browse files
[clang][extract-api] Suppprt for the module name property in SymbolGraph
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. Differential Revision: https://reviews.llvm.org/D122141
1 parent 2da5c57 commit 5ef2ec7

File tree

8 files changed

+33
-15
lines changed

8 files changed

+33
-15
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,6 +1091,8 @@ def interface_stub_version_EQ : JoinedOrSeparate<["-"], "interface-stub-version=
10911091
def exported__symbols__list : Separate<["-"], "exported_symbols_list">;
10921092
def extract_api : Flag<["-"], "extract-api">, Flags<[CC1Option]>, Group<Action_Group>,
10931093
HelpText<"Extract API information">;
1094+
def product_name_EQ: Joined<["--"], "product-name=">, Flags<[CC1Option]>,
1095+
MarshallingInfoString<FrontendOpts<"ProductName">>;
10941096
def e : JoinedOrSeparate<["-"], "e">, Flags<[LinkerInput]>, Group<Link_Group>;
10951097
def fmax_tokens_EQ : Joined<["-"], "fmax-tokens=">, Group<f_Group>, Flags<[CC1Option]>,
10961098
HelpText<"Max total number of preprocessed tokens for -Wmax-tokens.">,

clang/include/clang/ExtractAPI/Serialization/SerializerBase.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ class APISerializer {
3434

3535
protected:
3636
const APISet &API;
37+
38+
/// The product name of API.
39+
///
40+
/// Note: This should be used for populating metadata about the API.
41+
StringRef ProductName;
42+
3743
APISerializerOption Options;
3844

3945
public:
@@ -44,8 +50,9 @@ class APISerializer {
4450
APISerializer &operator=(APISerializer &&) = delete;
4551

4652
protected:
47-
APISerializer(const APISet &API, APISerializerOption Options = {})
48-
: API(API), Options(Options) {}
53+
APISerializer(const APISet &API, StringRef ProductName,
54+
APISerializerOption Options = {})
55+
: API(API), ProductName(ProductName), Options(Options) {}
4956

5057
virtual ~APISerializer() = default;
5158
};

clang/include/clang/ExtractAPI/Serialization/SymbolGraphSerializer.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,9 @@ class SymbolGraphSerializer : public APISerializer {
9090
void serializeGlobalRecord(const GlobalRecord &Record);
9191

9292
public:
93-
SymbolGraphSerializer(const APISet &API, APISerializerOption Options = {})
94-
: APISerializer(API, Options) {}
93+
SymbolGraphSerializer(const APISet &API, StringRef ProductName,
94+
APISerializerOption Options = {})
95+
: APISerializer(API, ProductName, Options) {}
9596
};
9697

9798
} // namespace extractapi

clang/include/clang/Frontend/FrontendOptions.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,10 @@ class FrontendOptions {
410410
/// The name of the action to run when using a plugin action.
411411
std::string ActionName;
412412

413+
// Currently this is only used as part of the `-extract-api` action.
414+
/// The name of the product the input files belong too.
415+
std::string ProductName;
416+
413417
/// Args to pass to the plugins
414418
std::map<std::string, std::vector<std::string>> PluginArgs;
415419

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4641,6 +4641,8 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
46414641
assert(JA.getType() == types::TY_API_INFO &&
46424642
"Extract API actions must generate a API information.");
46434643
CmdArgs.push_back("-extract-api");
4644+
if (Arg *ProductNameArg = Args.getLastArg(options::OPT_product_name_EQ))
4645+
ProductNameArg->render(Args, CmdArgs);
46444646
} else {
46454647
assert((isa<CompileJobAction>(JA) || isa<BackendJobAction>(JA)) &&
46464648
"Invalid action for clang tool.");

clang/lib/ExtractAPI/ExtractAPIConsumer.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,9 @@ class ExtractAPIVisitor : public RecursiveASTVisitor<ExtractAPIVisitor> {
183183

184184
class ExtractAPIConsumer : public ASTConsumer {
185185
public:
186-
ExtractAPIConsumer(ASTContext &Context, std::unique_ptr<raw_pwrite_stream> OS)
187-
: Visitor(Context), OS(std::move(OS)) {}
186+
ExtractAPIConsumer(ASTContext &Context, StringRef ProductName,
187+
std::unique_ptr<raw_pwrite_stream> OS)
188+
: Visitor(Context), ProductName(ProductName), OS(std::move(OS)) {}
188189

189190
void HandleTranslationUnit(ASTContext &Context) override {
190191
// Use ExtractAPIVisitor to traverse symbol declarations in the context.
@@ -193,12 +194,13 @@ class ExtractAPIConsumer : public ASTConsumer {
193194
// Setup a SymbolGraphSerializer to write out collected API information in
194195
// the Symbol Graph format.
195196
// FIXME: Make the kind of APISerializer configurable.
196-
SymbolGraphSerializer SGSerializer(Visitor.getAPI());
197+
SymbolGraphSerializer SGSerializer(Visitor.getAPI(), ProductName);
197198
SGSerializer.serialize(*OS);
198199
}
199200

200201
private:
201202
ExtractAPIVisitor Visitor;
203+
std::string ProductName;
202204
std::unique_ptr<raw_pwrite_stream> OS;
203205
};
204206

@@ -209,8 +211,9 @@ ExtractAPIAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
209211
std::unique_ptr<raw_pwrite_stream> OS = CreateOutputFile(CI, InFile);
210212
if (!OS)
211213
return nullptr;
212-
return std::make_unique<ExtractAPIConsumer>(CI.getASTContext(),
213-
std::move(OS));
214+
return std::make_unique<ExtractAPIConsumer>(
215+
CI.getASTContext(), CI.getInvocation().getFrontendOpts().ProductName,
216+
std::move(OS));
214217
}
215218

216219
std::unique_ptr<raw_pwrite_stream>

clang/lib/ExtractAPI/Serialization/SymbolGraphSerializer.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -376,10 +376,9 @@ Object SymbolGraphSerializer::serializeMetadata() const {
376376

377377
Object SymbolGraphSerializer::serializeModule() const {
378378
Object Module;
379-
// FIXME: We might not be building a module, some Clang-based languages might
380-
// not have a "module" concept. Figure out a way to provide a name to
381-
// describe the API set.
382-
Module["name"] = "";
379+
// The user is expected to always pass `--product-name=` on the command line
380+
// to populate this field.
381+
Module["name"] = ProductName;
383382
serializeObject(Module, "platform", serializePlatform(API.getTarget()));
384383
return Module;
385384
}

clang/test/ExtractAPI/global_record.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// RUN: split-file %s %t
33
// RUN: sed -e "s@INPUT_DIR@%/t@g" %t/reference.output.json.in >> \
44
// RUN: %t/reference.output.json
5-
// RUN: %clang -extract-api -target arm64-apple-macosx \
5+
// RUN: %clang -extract-api --product-name=GlobalRecord -target arm64-apple-macosx \
66
// RUN: %t/input.h -o %t/output.json | FileCheck -allow-empty %s
77

88
// Generator version is not consistent across test runs, normalize it.
@@ -37,7 +37,7 @@ char unavailable __attribute__((unavailable));
3737
"generator": "?"
3838
},
3939
"module": {
40-
"name": "",
40+
"name": "GlobalRecord",
4141
"platform": {
4242
"architecture": "arm64",
4343
"operatingSystem": {

0 commit comments

Comments
 (0)