[clang] [APINotes] Upstream Driver and Frontend options that enable API Notes (PR #73120)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Nov 22 05:57:59 PST 2023
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-driver
Author: Egor Zhdan (egorzhdan)
<details>
<summary>Changes</summary>
This upstreams more of the Clang API Notes functionality that is currently implemented in the Apple fork: https://github.com/apple/llvm-project/tree/next/clang/lib/APINotes
---
Full diff: https://github.com/llvm/llvm-project/pull/73120.diff
5 Files Affected:
- (modified) clang/include/clang/Basic/LangOptions.def (+1)
- (modified) clang/include/clang/Driver/Options.td (+13)
- (modified) clang/lib/Driver/ToolChains/Clang.cpp (+11)
- (modified) clang/lib/Frontend/CompilerInstance.cpp (+8)
- (modified) clang/lib/Frontend/CompilerInvocation.cpp (+23)
``````````diff
diff --git a/clang/include/clang/Basic/LangOptions.def b/clang/include/clang/Basic/LangOptions.def
index cd77b22bf3ace4b..c3d5399905a3fda 100644
--- a/clang/include/clang/Basic/LangOptions.def
+++ b/clang/include/clang/Basic/LangOptions.def
@@ -405,6 +405,7 @@ LANGOPT(XLPragmaPack, 1, 0, "IBM XL #pragma pack handling")
LANGOPT(RetainCommentsFromSystemHeaders, 1, 0, "retain documentation comments from system headers in the AST")
LANGOPT(APINotes, 1, 0, "use external API notes")
+LANGOPT(APINotesModules, 1, 0, "use module-based external API notes")
LANGOPT(SanitizeAddressFieldPadding, 2, 0, "controls how aggressive is ASan "
"field padding (0: none, 1:least "
diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index b2f2bcb6ac37910..ff2e4572ec4305f 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -1754,6 +1754,19 @@ def fswift_async_fp_EQ : Joined<["-"], "fswift-async-fp=">,
NormalizedValuesScope<"CodeGenOptions::SwiftAsyncFramePointerKind">,
NormalizedValues<["Auto", "Always", "Never"]>,
MarshallingInfoEnum<CodeGenOpts<"SwiftAsyncFramePointer">, "Always">;
+defm apinotes : BoolOption<"f", "apinotes",
+ LangOpts<"APINotes">, DefaultFalse,
+ PosFlag<SetTrue, [], [ClangOption], "Enable">,
+ NegFlag<SetFalse, [], [ClangOption], "Disable">,
+ BothFlags<[], [ClangOption, CC1Option], " external API notes support">>,
+ Group<f_clang_Group>;
+defm apinotes_modules : BoolOption<"f", "apinotes-modules",
+ LangOpts<"APINotesModules">, DefaultFalse,
+ PosFlag<SetTrue, [], [ClangOption], "Enable">,
+ NegFlag<SetFalse, [], [ClangOption], "Disable">,
+ BothFlags<[], [ClangOption, CC1Option],
+ " module-based external API notes support">>,
+ Group<f_clang_Group>;
def fapinotes_swift_version : Joined<["-"], "fapinotes-swift-version=">,
Group<f_clang_Group>, Visibility<[ClangOption, CC1Option]>,
MetaVarName<"<version>">,
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index 6dec117aed1056b..575ba57eef758db 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -6720,6 +6720,17 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
Args.addOptOutFlag(CmdArgs, options::OPT_fassume_sane_operator_new,
options::OPT_fno_assume_sane_operator_new);
+ if (Args.hasFlag(options::OPT_fapinotes, options::OPT_fno_apinotes, false) ||
+ Args.hasArg(options::OPT_iapinotes_modules)) {
+ if (Args.hasFlag(options::OPT_fapinotes, options::OPT_fno_apinotes, false))
+ CmdArgs.push_back("-fapinotes");
+ if (Args.hasFlag(options::OPT_fapinotes_modules,
+ options::OPT_fno_apinotes_modules, false))
+ CmdArgs.push_back("-fapinotes-modules");
+
+ Args.AddLastArg(CmdArgs, options::OPT_fapinotes_swift_version);
+ }
+
// -fblocks=0 is default.
if (Args.hasFlag(options::OPT_fblocks, options::OPT_fno_blocks,
TC.IsBlocksDefault()) ||
diff --git a/clang/lib/Frontend/CompilerInstance.cpp b/clang/lib/Frontend/CompilerInstance.cpp
index be5b38d6110fc3b..f9f839b3d391292 100644
--- a/clang/lib/Frontend/CompilerInstance.cpp
+++ b/clang/lib/Frontend/CompilerInstance.cpp
@@ -756,6 +756,14 @@ void CompilerInstance::createSema(TranslationUnitKind TUKind,
TheSema->addExternalSource(ExternalSemaSrc.get());
ExternalSemaSrc->InitializeSema(*TheSema);
}
+
+ // If we're building a module and are supposed to load API notes,
+ // notify the API notes manager.
+ if (auto currentModule = getPreprocessor().getCurrentModule()) {
+ (void)TheSema->APINotes.loadCurrentModuleAPINotes(
+ currentModule, getLangOpts().APINotesModules,
+ getAPINotesOpts().ModuleSearchPaths);
+ }
}
// Output Files
diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp
index 3f4ca02539080d0..3b92cc1281f9e0e 100644
--- a/clang/lib/Frontend/CompilerInvocation.cpp
+++ b/clang/lib/Frontend/CompilerInvocation.cpp
@@ -3267,6 +3267,16 @@ static bool ParseHeaderSearchArgs(HeaderSearchOptions &Opts, ArgList &Args,
return Diags.getNumErrors() == NumErrorsBefore;
}
+static void GenerateAPINotesArgs(const APINotesOptions &Opts,
+ ArgumentConsumer Consumer) {
+ if (!Opts.SwiftVersion.empty())
+ GenerateArg(Consumer, OPT_fapinotes_swift_version,
+ Opts.SwiftVersion.getAsString());
+
+ for (const auto &Path : Opts.ModuleSearchPaths)
+ GenerateArg(Consumer, OPT_iapinotes_modules, Path);
+}
+
static void ParseAPINotesArgs(APINotesOptions &Opts, ArgList &Args,
DiagnosticsEngine &diags) {
if (const Arg *A = Args.getLastArg(OPT_fapinotes_swift_version)) {
@@ -4746,6 +4756,18 @@ std::string CompilerInvocation::getModuleHash() const {
for (const auto &ext : getFrontendOpts().ModuleFileExtensions)
ext->hashExtension(HBuilder);
+ // Extend the signature with the Swift version for API notes.
+ const APINotesOptions &APINotesOpts = getAPINotesOpts();
+ if (!APINotesOpts.SwiftVersion.empty()) {
+ HBuilder.add(APINotesOpts.SwiftVersion.getMajor());
+ if (auto Minor = APINotesOpts.SwiftVersion.getMinor())
+ HBuilder.add(*Minor);
+ if (auto Subminor = APINotesOpts.SwiftVersion.getSubminor())
+ HBuilder.add(*Subminor);
+ if (auto Build = APINotesOpts.SwiftVersion.getBuild())
+ HBuilder.add(*Build);
+ }
+
// When compiling with -gmodules, also hash -fdebug-prefix-map as it
// affects the debug info in the PCM.
if (getCodeGenOpts().DebugTypeExtRefs)
@@ -4776,6 +4798,7 @@ void CompilerInvocationBase::generateCC1CommandLine(
GenerateFrontendArgs(getFrontendOpts(), Consumer, getLangOpts().IsHeaderFile);
GenerateTargetArgs(getTargetOpts(), Consumer);
GenerateHeaderSearchArgs(getHeaderSearchOpts(), Consumer);
+ GenerateAPINotesArgs(getAPINotesOpts(), Consumer);
GenerateLangArgs(getLangOpts(), Consumer, T, getFrontendOpts().DashX);
GenerateCodeGenArgs(getCodeGenOpts(), Consumer, T,
getFrontendOpts().OutputFile, &getLangOpts());
``````````
</details>
https://github.com/llvm/llvm-project/pull/73120
More information about the cfe-commits
mailing list