Skip to content

Commit 573e7ea

Browse files
committed
[APINotes] Upstream APINotesOptions
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 This adds the first compiler options related to API Notes to the upstream Clang: `-iapinotes-modules` and `-fapinotes-swift-version=`. However, this does not add the `-fapinotes` flag that enables API Notes, since the feature is not fully functional yet.
1 parent bc44e6e commit 573e7ea

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-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: 6 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; }
@@ -242,6 +247,7 @@ class CompilerInvocation : public CompilerInvocationBase {
242247
PreprocessorOptions &getPreprocessorOpts() { return *PPOpts; }
243248
AnalyzerOptions &getAnalyzerOpts() { return *AnalyzerOpts; }
244249
MigratorOptions &getMigratorOpts() { return *MigratorOpts; }
250+
APINotesOptions &getAPINotesOpts() { return *APINotesOpts; }
245251
CodeGenOptions &getCodeGenOpts() { return *CodeGenOpts; }
246252
FileSystemOptions &getFileSystemOpts() { return *FSOpts; }
247253
FrontendOptions &getFrontendOpts() { return *FrontendOpts; }

clang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3261,6 +3261,17 @@ static bool ParseHeaderSearchArgs(HeaderSearchOptions &Opts, ArgList &Args,
32613261
return Diags.getNumErrors() == NumErrorsBefore;
32623262
}
32633263

3264+
static void ParseAPINotesArgs(APINotesOptions &Opts, ArgList &Args,
3265+
DiagnosticsEngine &diags) {
3266+
if (const Arg *A = Args.getLastArg(OPT_fapinotes_swift_version)) {
3267+
if (Opts.SwiftVersion.tryParse(A->getValue()))
3268+
diags.Report(diag::err_drv_invalid_value)
3269+
<< A->getAsString(Args) << A->getValue();
3270+
}
3271+
for (const Arg *A : Args.filtered(OPT_iapinotes_modules))
3272+
Opts.ModuleSearchPaths.push_back(A->getValue());
3273+
}
3274+
32643275
/// Check if input file kind and language standard are compatible.
32653276
static bool IsInputCompatibleWithStandard(InputKind IK,
32663277
const LangStandard &S) {
@@ -4538,6 +4549,7 @@ bool CompilerInvocation::CreateFromArgsImpl(
45384549
llvm::Triple T(Res.getTargetOpts().Triple);
45394550
ParseHeaderSearchArgs(Res.getHeaderSearchOpts(), Args, Diags,
45404551
Res.getFileSystemOpts().WorkingDir);
4552+
ParseAPINotesArgs(Res.getAPINotesOpts(), Args, Diags);
45414553

45424554
ParseLangArgs(LangOpts, Args, DashX, T, Res.getPreprocessorOpts().Includes,
45434555
Diags);

0 commit comments

Comments
 (0)