Skip to content

Commit

Permalink
[APINotes] Upstream Driver and Frontend options that enable API Notes
Browse files Browse the repository at this point in the history
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
  • Loading branch information
egorzhdan authored Nov 23, 2023
1 parent 258631f commit 07d799f
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 0 deletions.
1 change: 1 addition & 0 deletions clang/include/clang/Basic/LangOptions.def
Original file line number Diff line number Diff line change
Expand Up @@ -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 "
Expand Down
12 changes: 12 additions & 0 deletions clang/include/clang/Driver/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -1754,6 +1754,18 @@ 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>">,
Expand Down
7 changes: 7 additions & 0 deletions clang/lib/Driver/ToolChains/Clang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6720,6 +6720,13 @@ 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))
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()) ||
Expand Down
8 changes: 8 additions & 0 deletions clang/lib/Frontend/CompilerInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions clang/lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit 07d799f

Please sign in to comment.