Skip to content

Commit c51095f

Browse files
authored
[InstallAPI] Add installapi specific options & diagnostics (llvm#85100)
* A lot of `tapi installapi` options are already shared with clang, but not all. This patch handles installapi-specific options by filtering for them in the initial argv input, then passing the rest to the clang driver. * Installapi not only generates a text file but also reports to library developers when there are inconsistencies between an interface and its implementation. To allow this, add support for reporting installapi diagnostics. This will be leveraged in the verifier service.
1 parent f694f63 commit c51095f

17 files changed

+347
-55
lines changed

clang/include/clang/Basic/AllDiagnostics.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "clang/Basic/DiagnosticCrossTU.h"
2121
#include "clang/Basic/DiagnosticDriver.h"
2222
#include "clang/Basic/DiagnosticFrontend.h"
23+
#include "clang/Basic/DiagnosticInstallAPI.h"
2324
#include "clang/Basic/DiagnosticLex.h"
2425
#include "clang/Basic/DiagnosticParse.h"
2526
#include "clang/Basic/DiagnosticSema.h"

clang/include/clang/Basic/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ clang_diag_gen(Common)
1212
clang_diag_gen(CrossTU)
1313
clang_diag_gen(Driver)
1414
clang_diag_gen(Frontend)
15+
clang_diag_gen(InstallAPI)
1516
clang_diag_gen(Lex)
1617
clang_diag_gen(Parse)
1718
clang_diag_gen(Refactoring)

clang/include/clang/Basic/Diagnostic.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ include "DiagnosticCommonKinds.td"
162162
include "DiagnosticCrossTUKinds.td"
163163
include "DiagnosticDriverKinds.td"
164164
include "DiagnosticFrontendKinds.td"
165+
include "DiagnosticInstallAPIKinds.td"
165166
include "DiagnosticLexKinds.td"
166167
include "DiagnosticParseKinds.td"
167168
include "DiagnosticRefactoringKinds.td"

clang/include/clang/Basic/DiagnosticIDs.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ namespace clang {
4242
DIAG_SIZE_SEMA = 4500,
4343
DIAG_SIZE_ANALYSIS = 100,
4444
DIAG_SIZE_REFACTORING = 1000,
45+
DIAG_SIZE_INSTALLAPI = 100,
4546
};
4647
// Start position for diagnostics.
4748
enum {
@@ -57,7 +58,8 @@ namespace clang {
5758
DIAG_START_SEMA = DIAG_START_CROSSTU + static_cast<int>(DIAG_SIZE_CROSSTU),
5859
DIAG_START_ANALYSIS = DIAG_START_SEMA + static_cast<int>(DIAG_SIZE_SEMA),
5960
DIAG_START_REFACTORING = DIAG_START_ANALYSIS + static_cast<int>(DIAG_SIZE_ANALYSIS),
60-
DIAG_UPPER_LIMIT = DIAG_START_REFACTORING + static_cast<int>(DIAG_SIZE_REFACTORING)
61+
DIAG_START_INSTALLAPI = DIAG_START_REFACTORING + static_cast<int>(DIAG_SIZE_REFACTORING),
62+
DIAG_UPPER_LIMIT = DIAG_START_INSTALLAPI + static_cast<int>(DIAG_SIZE_INSTALLAPI)
6163
};
6264

6365
class CustomDiagInfo;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//===--- DiagnosticInstallAPI.h - Diagnostics for InstallAPI-----*- 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_BASIC_DIAGNOSTICINSTALLAPI_H
10+
#define LLVM_CLANG_BASIC_DIAGNOSTICINSTALLAPI_H
11+
12+
#include "clang/Basic/Diagnostic.h"
13+
namespace clang {
14+
namespace diag {
15+
enum {
16+
#define DIAG(ENUM, FLAGS, DEFAULT_MAPPING, DESC, GROUP, SFINAE, NOWERROR, \
17+
SHOWINSYSHEADER, SHOWINSYSMACRO, DEFERRABLE, CATEGORY) \
18+
ENUM,
19+
#define INSTALLAPISTART
20+
#include "clang/Basic/DiagnosticInstallAPIKinds.inc"
21+
#undef DIAG
22+
NUM_BUILTIN_INSTALLAPI_DIAGNOSTICS
23+
};
24+
} // namespace diag
25+
} // namespace clang
26+
#endif // LLVM_CLANG_BASIC_DIAGNOSTICINSTALLAPI_H
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//==--- DiagnosticInstallAPIKinds.td - installapi diagnostics -------------===//
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+
//===----------------------------------------------------------------------===//
10+
// InstallAPI Diagnostics
11+
//===----------------------------------------------------------------------===//
12+
13+
let Component = "InstallAPI" in {
14+
let CategoryName = "Command line" in {
15+
def err_cannot_write_file : Error<"cannot write file '%0': %1">;
16+
def err_no_install_name : Error<"no install name specified: add -install_name <path>">;
17+
def err_no_output_file: Error<"no output file specified">;
18+
} // end of command line category.
19+
20+
} // end of InstallAPI component
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//===- InstallAPI/DylibVerifier.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_INSTALLAPI_DYLIBVERIFIER_H
10+
#define LLVM_CLANG_INSTALLAPI_DYLIBVERIFIER_H
11+
12+
#include "llvm/TextAPI/Target.h"
13+
14+
namespace clang {
15+
namespace installapi {
16+
17+
/// A list of InstallAPI verification modes.
18+
enum class VerificationMode {
19+
Invalid,
20+
ErrorsOnly,
21+
ErrorsAndWarnings,
22+
Pedantic,
23+
};
24+
25+
} // namespace installapi
26+
} // namespace clang
27+
#endif // LLVM_CLANG_INSTALLAPI_DYLIBVERIFIER_H
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//===--- InstallAPIDiagnostic.h - Diagnostics for InstallAPI ----*- 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_INSTALLAPI_INSTALLAPIDIAGNOSTIC_H
10+
#define LLVM_CLANG_INSTALLAPI_INSTALLAPIDIAGNOSTIC_H
11+
12+
#include "clang/Basic/DiagnosticInstallAPI.h"
13+
14+
#endif

clang/lib/Basic/DiagnosticIDs.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ struct StaticDiagInfoDescriptionStringTable {
4949
#include "clang/Basic/DiagnosticSemaKinds.inc"
5050
#include "clang/Basic/DiagnosticAnalysisKinds.inc"
5151
#include "clang/Basic/DiagnosticRefactoringKinds.inc"
52+
#include "clang/Basic/DiagnosticInstallAPIKinds.inc"
5253
// clang-format on
5354
#undef DIAG
5455
};
@@ -70,7 +71,8 @@ const StaticDiagInfoDescriptionStringTable StaticDiagInfoDescriptions = {
7071
#include "clang/Basic/DiagnosticSemaKinds.inc"
7172
#include "clang/Basic/DiagnosticAnalysisKinds.inc"
7273
#include "clang/Basic/DiagnosticRefactoringKinds.inc"
73-
// clang-format on
74+
#include "clang/Basic/DiagnosticInstallAPIKinds.inc"
75+
// clang-format on
7476
#undef DIAG
7577
};
7678

@@ -95,7 +97,8 @@ const uint32_t StaticDiagInfoDescriptionOffsets[] = {
9597
#include "clang/Basic/DiagnosticSemaKinds.inc"
9698
#include "clang/Basic/DiagnosticAnalysisKinds.inc"
9799
#include "clang/Basic/DiagnosticRefactoringKinds.inc"
98-
// clang-format on
100+
#include "clang/Basic/DiagnosticInstallAPIKinds.inc"
101+
// clang-format on
99102
#undef DIAG
100103
};
101104

@@ -173,6 +176,7 @@ VALIDATE_DIAG_SIZE(CROSSTU)
173176
VALIDATE_DIAG_SIZE(SEMA)
174177
VALIDATE_DIAG_SIZE(ANALYSIS)
175178
VALIDATE_DIAG_SIZE(REFACTORING)
179+
VALIDATE_DIAG_SIZE(INSTALLAPI)
176180
#undef VALIDATE_DIAG_SIZE
177181
#undef STRINGIFY_NAME
178182

@@ -204,6 +208,7 @@ const StaticDiagInfoRec StaticDiagInfo[] = {
204208
#include "clang/Basic/DiagnosticSemaKinds.inc"
205209
#include "clang/Basic/DiagnosticAnalysisKinds.inc"
206210
#include "clang/Basic/DiagnosticRefactoringKinds.inc"
211+
#include "clang/Basic/DiagnosticInstallAPIKinds.inc"
207212
// clang-format on
208213
#undef DIAG
209214
};
@@ -246,6 +251,7 @@ CATEGORY(CROSSTU, COMMENT)
246251
CATEGORY(SEMA, CROSSTU)
247252
CATEGORY(ANALYSIS, SEMA)
248253
CATEGORY(REFACTORING, ANALYSIS)
254+
CATEGORY(INSTALLAPI, REFACTORING)
249255
#undef CATEGORY
250256

251257
// Avoid out of bounds reads.
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
/// Check non-darwin triple is rejected.
2-
// RUN: not clang-installapi -target x86_64-unknown-unknown %s 2> %t
2+
// RUN: not clang-installapi -target x86_64-unknown-unknown %s -o tmp.tbd 2> %t
33
// RUN: FileCheck --check-prefix INVALID_INSTALLAPI -input-file %t %s
44
// INVALID_INSTALLAPI: error: unsupported option 'installapi' for target 'x86_64-unknown-unknown'
5+
6+
/// Check that missing install_name is reported.
7+
// RUN: not clang-installapi -target x86_64-apple-ios-simulator %s -o tmp.tbd 2> %t
8+
// RUN: FileCheck --check-prefix INVALID_INSTALL_NAME -input-file %t %s
9+
// INVALID_INSTALL_NAME: error: no install name specified: add -install_name <path>

clang/test/InstallAPI/functions.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
// RUN: clang-installapi -target arm64-apple-macos13.1 \
66
// RUN: -I%t/usr/include -I%t/usr/local/include \
7-
// RUN: -install_name @rpath/lib/libfunctions.dylib \
7+
// RUN: -install_name @rpath/lib/libfunctions.dylib --filetype=tbd-v4 \
88
// RUN: %t/inputs.json -o %t/outputs.tbd 2>&1 | FileCheck %s --allow-empty
99
// RUN: llvm-readtapi -compare %t/outputs.tbd %t/expected.tbd 2>&1 | FileCheck %s --allow-empty
1010

clang/tools/clang-installapi/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,20 @@ set(LLVM_LINK_COMPONENTS
22
Support
33
TargetParser
44
TextAPI
5+
TextAPIBinaryReader
56
Option
67
)
78

9+
set(LLVM_TARGET_DEFINITIONS InstallAPIOpts.td)
10+
tablegen(LLVM InstallAPIOpts.inc -gen-opt-parser-defs)
11+
add_public_tablegen_target(InstallAPIDriverOptions)
12+
813
add_clang_tool(clang-installapi
914
ClangInstallAPI.cpp
1015
Options.cpp
1116

17+
DEPENDS
18+
InstallAPIDriverOptions
1219
GENERATE_DRIVER
1320
)
1421

@@ -22,3 +29,4 @@ clang_target_link_libraries(clang-installapi
2229
clangTooling
2330
clangSerialization
2431
)
32+

clang/tools/clang-installapi/ClangInstallAPI.cpp

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
#include "Options.h"
1515
#include "clang/Basic/Diagnostic.h"
1616
#include "clang/Basic/DiagnosticFrontend.h"
17-
#include "clang/Driver/Driver.h"
1817
#include "clang/Driver/DriverDiagnostic.h"
1918
#include "clang/Driver/Tool.h"
2019
#include "clang/Frontend/TextDiagnosticPrinter.h"
2120
#include "clang/InstallAPI/Frontend.h"
2221
#include "clang/InstallAPI/FrontendRecords.h"
22+
#include "clang/InstallAPI/InstallAPIDiagnostic.h"
2323
#include "clang/InstallAPI/MachO.h"
2424
#include "clang/Tooling/Tooling.h"
2525
#include "llvm/ADT/ArrayRef.h"
@@ -92,22 +92,8 @@ static bool run(ArrayRef<const char *> Args, const char *ProgName) {
9292
IntrusiveRefCntPtr<clang::FileManager> FM(
9393
new FileManager(clang::FileSystemOptions(), OverlayFileSystem));
9494

95-
// Set up driver to parse input arguments.
96-
auto DriverArgs = llvm::ArrayRef(Args).slice(1);
97-
clang::driver::Driver Driver(ProgName, llvm::sys::getDefaultTargetTriple(),
98-
*Diag, "clang installapi tool");
99-
auto TargetAndMode =
100-
clang::driver::ToolChain::getTargetAndModeFromProgramName(ProgName);
101-
Driver.setTargetAndMode(TargetAndMode);
102-
bool HasError = false;
103-
llvm::opt::InputArgList ArgList =
104-
Driver.ParseArgStrings(DriverArgs, /*UseDriverMode=*/true, HasError);
105-
if (HasError)
106-
return EXIT_FAILURE;
107-
Driver.setCheckInputsExist(false);
108-
109-
// Capture InstallAPI specific options and diagnose any option errors.
110-
Options Opts(*Diag, FM.get(), ArgList);
95+
// Capture all options and diagnose any errors.
96+
Options Opts(*Diag, FM.get(), Args, ProgName);
11197
if (Diag->hasErrorOccurred())
11298
return EXIT_FAILURE;
11399

@@ -161,7 +147,8 @@ static bool run(ArrayRef<const char *> Args, const char *ProgName) {
161147

162148
// Write output file and perform CI cleanup.
163149
if (auto Err = TextAPIWriter::writeToStream(*Out, IF, Ctx.FT)) {
164-
Diag->Report(diag::err_cannot_open_file) << Ctx.OutputLoc;
150+
Diag->Report(diag::err_cannot_write_file)
151+
<< Ctx.OutputLoc << std::move(Err);
165152
CI->clearOutputFiles(/*EraseFiles=*/true);
166153
return EXIT_FAILURE;
167154
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//===--- InstallAPIOpts.td ------------------------------------------------===//
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+
// This file defines the specific options for InstallAPI.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
// Include the common option parsing interfaces.
14+
include "llvm/Option/OptParser.td"
15+
16+
17+
/////////
18+
// Options
19+
20+
// TextAPI options.
21+
def filetype : Joined<["--"], "filetype=">,
22+
HelpText<"Specify the output file type (tbd-v4 or tbd-v5)">;
23+
24+
// Verification options.
25+
def verify_against : Separate<["-"], "verify-against">,
26+
HelpText<"Verify the specified dynamic library/framework against the headers">;
27+
def verify_against_EQ : Joined<["--"], "verify-against=">, Alias<verify_against>;
28+
def verify_mode_EQ : Joined<["--"], "verify-mode=">,
29+
HelpText<"Specify the severity and extend of the validation. Valid modes are ErrorsOnly, ErrorsAndWarnings, and Pedantic.">;
30+
def demangle : Flag<["--", "-"], "demangle">,
31+
HelpText<"Demangle symbols when printing warnings and errors">;

0 commit comments

Comments
 (0)