Skip to content

Commit f1c9a1c

Browse files
authored
Revert "Revert "[APINotes] Upstream APINotesOptions""
This re-lands #70827 while preventing the assertion failure that occurred when generating `ASTNodeAPI.json` on non-Apple platforms.
1 parent 98da183 commit f1c9a1c

File tree

4 files changed

+68
-0
lines changed

4 files changed

+68
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//===--- APINotesOptions.h --------------------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_CLANG_APINOTES_APINOTESOPTIONS_H
10+
#define LLVM_CLANG_APINOTES_APINOTESOPTIONS_H
11+
12+
#include "llvm/Support/VersionTuple.h"
13+
#include <string>
14+
#include <vector>
15+
16+
namespace clang {
17+
18+
/// Tracks various options which control how API notes are found and handled.
19+
class APINotesOptions {
20+
public:
21+
/// The Swift version which should be used for API notes.
22+
llvm::VersionTuple SwiftVersion;
23+
24+
/// The set of search paths where we API notes can be found for particular
25+
/// modules.
26+
///
27+
/// The API notes in this directory are stored as <ModuleName>.apinotes, and
28+
/// are only applied when building the module <ModuleName>.
29+
std::vector<std::string> ModuleSearchPaths;
30+
};
31+
32+
} // namespace clang
33+
34+
#endif // LLVM_CLANG_APINOTES_APINOTESOPTIONS_H

clang/include/clang/Driver/Options.td

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1733,6 +1733,10 @@ def fswift_async_fp_EQ : Joined<["-"], "fswift-async-fp=">,
17331733
NormalizedValuesScope<"CodeGenOptions::SwiftAsyncFramePointerKind">,
17341734
NormalizedValues<["Auto", "Always", "Never"]>,
17351735
MarshallingInfoEnum<CodeGenOpts<"SwiftAsyncFramePointer">, "Always">;
1736+
def fapinotes_swift_version : Joined<["-"], "fapinotes-swift-version=">,
1737+
Group<f_clang_Group>, Visibility<[ClangOption, CC1Option]>,
1738+
MetaVarName<"<version>">,
1739+
HelpText<"Specify the Swift version to use when filtering API notes">;
17361740

17371741
defm addrsig : BoolFOption<"addrsig",
17381742
CodeGenOpts<"Addrsig">, DefaultFalse,
@@ -4129,6 +4133,9 @@ def ibuiltininc : Flag<["-"], "ibuiltininc">, Group<clang_i_Group>,
41294133
def index_header_map : Flag<["-"], "index-header-map">,
41304134
Visibility<[ClangOption, CC1Option]>,
41314135
HelpText<"Make the next included directory (-I or -F) an indexer header map">;
4136+
def iapinotes_modules : JoinedOrSeparate<["-"], "iapinotes-modules">, Group<clang_i_Group>,
4137+
Visibility<[ClangOption, CC1Option]>,
4138+
HelpText<"Add directory to the API notes search path referenced by module name">, MetaVarName<"<directory>">;
41324139
def idirafter : JoinedOrSeparate<["-"], "idirafter">, Group<clang_i_Group>,
41334140
Visibility<[ClangOption, CC1Option]>,
41344141
HelpText<"Add directory to AFTER include search path">;

clang/include/clang/Frontend/CompilerInvocation.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#ifndef LLVM_CLANG_FRONTEND_COMPILERINVOCATION_H
1010
#define LLVM_CLANG_FRONTEND_COMPILERINVOCATION_H
1111

12+
#include "clang/APINotes/APINotesOptions.h"
1213
#include "clang/Basic/CodeGenOptions.h"
1314
#include "clang/Basic/DiagnosticOptions.h"
1415
#include "clang/Basic/FileSystemOptions.h"
@@ -92,6 +93,9 @@ class CompilerInvocationBase {
9293

9394
std::shared_ptr<MigratorOptions> MigratorOpts;
9495

96+
/// Options controlling API notes.
97+
std::shared_ptr<APINotesOptions> APINotesOpts;
98+
9599
/// Options controlling IRgen and the backend.
96100
std::shared_ptr<CodeGenOptions> CodeGenOpts;
97101

@@ -131,6 +135,7 @@ class CompilerInvocationBase {
131135
const PreprocessorOptions &getPreprocessorOpts() const { return *PPOpts; }
132136
const AnalyzerOptions &getAnalyzerOpts() const { return *AnalyzerOpts; }
133137
const MigratorOptions &getMigratorOpts() const { return *MigratorOpts; }
138+
const APINotesOptions &getAPINotesOpts() const { return *APINotesOpts; }
134139
const CodeGenOptions &getCodeGenOpts() const { return *CodeGenOpts; }
135140
const FileSystemOptions &getFileSystemOpts() const { return *FSOpts; }
136141
const FrontendOptions &getFrontendOpts() const { return *FrontendOpts; }
@@ -226,6 +231,7 @@ class CompilerInvocation : public CompilerInvocationBase {
226231
using CompilerInvocationBase::getPreprocessorOpts;
227232
using CompilerInvocationBase::getAnalyzerOpts;
228233
using CompilerInvocationBase::getMigratorOpts;
234+
using CompilerInvocationBase::getAPINotesOpts;
229235
using CompilerInvocationBase::getCodeGenOpts;
230236
using CompilerInvocationBase::getFileSystemOpts;
231237
using CompilerInvocationBase::getFrontendOpts;
@@ -242,6 +248,7 @@ class CompilerInvocation : public CompilerInvocationBase {
242248
PreprocessorOptions &getPreprocessorOpts() { return *PPOpts; }
243249
AnalyzerOptions &getAnalyzerOpts() { return *AnalyzerOpts; }
244250
MigratorOptions &getMigratorOpts() { return *MigratorOpts; }
251+
APINotesOptions &getAPINotesOpts() { return *APINotesOpts; }
245252
CodeGenOptions &getCodeGenOpts() { return *CodeGenOpts; }
246253
FileSystemOptions &getFileSystemOpts() { return *FSOpts; }
247254
FrontendOptions &getFrontendOpts() { return *FrontendOpts; }
@@ -368,6 +375,7 @@ class CowCompilerInvocation : public CompilerInvocationBase {
368375
PreprocessorOptions &getMutPreprocessorOpts();
369376
AnalyzerOptions &getMutAnalyzerOpts();
370377
MigratorOptions &getMutMigratorOpts();
378+
APINotesOptions &getMutAPINotesOpts();
371379
CodeGenOptions &getMutCodeGenOpts();
372380
FileSystemOptions &getMutFileSystemOpts();
373381
FrontendOptions &getMutFrontendOpts();

clang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ CompilerInvocationBase::CompilerInvocationBase()
145145
PPOpts(std::make_shared<PreprocessorOptions>()),
146146
AnalyzerOpts(llvm::makeIntrusiveRefCnt<AnalyzerOptions>()),
147147
MigratorOpts(std::make_shared<MigratorOptions>()),
148+
APINotesOpts(std::make_shared<APINotesOptions>()),
148149
CodeGenOpts(std::make_shared<CodeGenOptions>()),
149150
FSOpts(std::make_shared<FileSystemOptions>()),
150151
FrontendOpts(std::make_shared<FrontendOptions>()),
@@ -161,6 +162,7 @@ CompilerInvocationBase::deep_copy_assign(const CompilerInvocationBase &X) {
161162
PPOpts = make_shared_copy(X.getPreprocessorOpts());
162163
AnalyzerOpts = makeIntrusiveRefCntCopy(X.getAnalyzerOpts());
163164
MigratorOpts = make_shared_copy(X.getMigratorOpts());
165+
APINotesOpts = make_shared_copy(X.getAPINotesOpts());
164166
CodeGenOpts = make_shared_copy(X.getCodeGenOpts());
165167
FSOpts = make_shared_copy(X.getFileSystemOpts());
166168
FrontendOpts = make_shared_copy(X.getFrontendOpts());
@@ -180,6 +182,7 @@ CompilerInvocationBase::shallow_copy_assign(const CompilerInvocationBase &X) {
180182
PPOpts = X.PPOpts;
181183
AnalyzerOpts = X.AnalyzerOpts;
182184
MigratorOpts = X.MigratorOpts;
185+
APINotesOpts = X.APINotesOpts;
183186
CodeGenOpts = X.CodeGenOpts;
184187
FSOpts = X.FSOpts;
185188
FrontendOpts = X.FrontendOpts;
@@ -233,6 +236,10 @@ MigratorOptions &CowCompilerInvocation::getMutMigratorOpts() {
233236
return ensureOwned(MigratorOpts);
234237
}
235238

239+
APINotesOptions &CowCompilerInvocation::getMutAPINotesOpts() {
240+
return ensureOwned(APINotesOpts);
241+
}
242+
236243
CodeGenOptions &CowCompilerInvocation::getMutCodeGenOpts() {
237244
return ensureOwned(CodeGenOpts);
238245
}
@@ -3261,6 +3268,17 @@ static bool ParseHeaderSearchArgs(HeaderSearchOptions &Opts, ArgList &Args,
32613268
return Diags.getNumErrors() == NumErrorsBefore;
32623269
}
32633270

3271+
static void ParseAPINotesArgs(APINotesOptions &Opts, ArgList &Args,
3272+
DiagnosticsEngine &diags) {
3273+
if (const Arg *A = Args.getLastArg(OPT_fapinotes_swift_version)) {
3274+
if (Opts.SwiftVersion.tryParse(A->getValue()))
3275+
diags.Report(diag::err_drv_invalid_value)
3276+
<< A->getAsString(Args) << A->getValue();
3277+
}
3278+
for (const Arg *A : Args.filtered(OPT_iapinotes_modules))
3279+
Opts.ModuleSearchPaths.push_back(A->getValue());
3280+
}
3281+
32643282
/// Check if input file kind and language standard are compatible.
32653283
static bool IsInputCompatibleWithStandard(InputKind IK,
32663284
const LangStandard &S) {
@@ -4538,6 +4556,7 @@ bool CompilerInvocation::CreateFromArgsImpl(
45384556
llvm::Triple T(Res.getTargetOpts().Triple);
45394557
ParseHeaderSearchArgs(Res.getHeaderSearchOpts(), Args, Diags,
45404558
Res.getFileSystemOpts().WorkingDir);
4559+
ParseAPINotesArgs(Res.getAPINotesOpts(), Args, Diags);
45414560

45424561
ParseLangArgs(LangOpts, Args, DashX, T, Res.getPreprocessorOpts().Includes,
45434562
Diags);

0 commit comments

Comments
 (0)