Skip to content

Commit 07d799f

Browse files
authored
[APINotes] Upstream Driver and Frontend options that enable API Notes
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
1 parent 258631f commit 07d799f

File tree

5 files changed

+51
-0
lines changed

5 files changed

+51
-0
lines changed

clang/include/clang/Basic/LangOptions.def

+1
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,7 @@ LANGOPT(XLPragmaPack, 1, 0, "IBM XL #pragma pack handling")
405405
LANGOPT(RetainCommentsFromSystemHeaders, 1, 0, "retain documentation comments from system headers in the AST")
406406

407407
LANGOPT(APINotes, 1, 0, "use external API notes")
408+
LANGOPT(APINotesModules, 1, 0, "use module-based external API notes")
408409

409410
LANGOPT(SanitizeAddressFieldPadding, 2, 0, "controls how aggressive is ASan "
410411
"field padding (0: none, 1:least "

clang/include/clang/Driver/Options.td

+12
Original file line numberDiff line numberDiff line change
@@ -1754,6 +1754,18 @@ def fswift_async_fp_EQ : Joined<["-"], "fswift-async-fp=">,
17541754
NormalizedValuesScope<"CodeGenOptions::SwiftAsyncFramePointerKind">,
17551755
NormalizedValues<["Auto", "Always", "Never"]>,
17561756
MarshallingInfoEnum<CodeGenOpts<"SwiftAsyncFramePointer">, "Always">;
1757+
defm apinotes : BoolOption<"f", "apinotes",
1758+
LangOpts<"APINotes">, DefaultFalse,
1759+
PosFlag<SetTrue, [], [ClangOption], "Enable">,
1760+
NegFlag<SetFalse, [], [ClangOption], "Disable">,
1761+
BothFlags<[], [ClangOption, CC1Option], "external API notes support">>,
1762+
Group<f_clang_Group>;
1763+
defm apinotes_modules : BoolOption<"f", "apinotes-modules",
1764+
LangOpts<"APINotesModules">, DefaultFalse,
1765+
PosFlag<SetTrue, [], [ClangOption], "Enable">,
1766+
NegFlag<SetFalse, [], [ClangOption], "Disable">,
1767+
BothFlags<[], [ClangOption, CC1Option], "module-based external API notes support">>,
1768+
Group<f_clang_Group>;
17571769
def fapinotes_swift_version : Joined<["-"], "fapinotes-swift-version=">,
17581770
Group<f_clang_Group>, Visibility<[ClangOption, CC1Option]>,
17591771
MetaVarName<"<version>">,

clang/lib/Driver/ToolChains/Clang.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -6720,6 +6720,13 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
67206720
Args.addOptOutFlag(CmdArgs, options::OPT_fassume_sane_operator_new,
67216721
options::OPT_fno_assume_sane_operator_new);
67226722

6723+
if (Args.hasFlag(options::OPT_fapinotes, options::OPT_fno_apinotes, false))
6724+
CmdArgs.push_back("-fapinotes");
6725+
if (Args.hasFlag(options::OPT_fapinotes_modules,
6726+
options::OPT_fno_apinotes_modules, false))
6727+
CmdArgs.push_back("-fapinotes-modules");
6728+
Args.AddLastArg(CmdArgs, options::OPT_fapinotes_swift_version);
6729+
67236730
// -fblocks=0 is default.
67246731
if (Args.hasFlag(options::OPT_fblocks, options::OPT_fno_blocks,
67256732
TC.IsBlocksDefault()) ||

clang/lib/Frontend/CompilerInstance.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,14 @@ void CompilerInstance::createSema(TranslationUnitKind TUKind,
756756
TheSema->addExternalSource(ExternalSemaSrc.get());
757757
ExternalSemaSrc->InitializeSema(*TheSema);
758758
}
759+
760+
// If we're building a module and are supposed to load API notes,
761+
// notify the API notes manager.
762+
if (auto *currentModule = getPreprocessor().getCurrentModule()) {
763+
(void)TheSema->APINotes.loadCurrentModuleAPINotes(
764+
currentModule, getLangOpts().APINotesModules,
765+
getAPINotesOpts().ModuleSearchPaths);
766+
}
759767
}
760768

761769
// Output Files

clang/lib/Frontend/CompilerInvocation.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -3267,6 +3267,16 @@ static bool ParseHeaderSearchArgs(HeaderSearchOptions &Opts, ArgList &Args,
32673267
return Diags.getNumErrors() == NumErrorsBefore;
32683268
}
32693269

3270+
static void GenerateAPINotesArgs(const APINotesOptions &Opts,
3271+
ArgumentConsumer Consumer) {
3272+
if (!Opts.SwiftVersion.empty())
3273+
GenerateArg(Consumer, OPT_fapinotes_swift_version,
3274+
Opts.SwiftVersion.getAsString());
3275+
3276+
for (const auto &Path : Opts.ModuleSearchPaths)
3277+
GenerateArg(Consumer, OPT_iapinotes_modules, Path);
3278+
}
3279+
32703280
static void ParseAPINotesArgs(APINotesOptions &Opts, ArgList &Args,
32713281
DiagnosticsEngine &diags) {
32723282
if (const Arg *A = Args.getLastArg(OPT_fapinotes_swift_version)) {
@@ -4746,6 +4756,18 @@ std::string CompilerInvocation::getModuleHash() const {
47464756
for (const auto &ext : getFrontendOpts().ModuleFileExtensions)
47474757
ext->hashExtension(HBuilder);
47484758

4759+
// Extend the signature with the Swift version for API notes.
4760+
const APINotesOptions &APINotesOpts = getAPINotesOpts();
4761+
if (!APINotesOpts.SwiftVersion.empty()) {
4762+
HBuilder.add(APINotesOpts.SwiftVersion.getMajor());
4763+
if (auto Minor = APINotesOpts.SwiftVersion.getMinor())
4764+
HBuilder.add(*Minor);
4765+
if (auto Subminor = APINotesOpts.SwiftVersion.getSubminor())
4766+
HBuilder.add(*Subminor);
4767+
if (auto Build = APINotesOpts.SwiftVersion.getBuild())
4768+
HBuilder.add(*Build);
4769+
}
4770+
47494771
// When compiling with -gmodules, also hash -fdebug-prefix-map as it
47504772
// affects the debug info in the PCM.
47514773
if (getCodeGenOpts().DebugTypeExtRefs)
@@ -4776,6 +4798,7 @@ void CompilerInvocationBase::generateCC1CommandLine(
47764798
GenerateFrontendArgs(getFrontendOpts(), Consumer, getLangOpts().IsHeaderFile);
47774799
GenerateTargetArgs(getTargetOpts(), Consumer);
47784800
GenerateHeaderSearchArgs(getHeaderSearchOpts(), Consumer);
4801+
GenerateAPINotesArgs(getAPINotesOpts(), Consumer);
47794802
GenerateLangArgs(getLangOpts(), Consumer, T, getFrontendOpts().DashX);
47804803
GenerateCodeGenArgs(getCodeGenOpts(), Consumer, T,
47814804
getFrontendOpts().OutputFile, &getLangOpts());

0 commit comments

Comments
 (0)