Skip to content

Commit 1857a37

Browse files
authored
Merge pull request #26773 from nkcsgexi/abort-compiler-error
ABI/API checker: abort after error occurs while importing a module
2 parents 84af118 + ce57492 commit 1857a37

File tree

6 files changed

+64
-20
lines changed

6 files changed

+64
-20
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"kind": "Root",
3+
"name": "TopLevel",
4+
"printedName": "TopLevel",
5+
"json_format_version": 1
6+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// REQUIRES: OS=macosx
2+
// RUN: %empty-directory(%t.mod)
3+
// RUN: %empty-directory(%t.sdk)
4+
// RUN: %empty-directory(%t.module-cache)
5+
// RUN: %api-digester -generate-empty-baseline -o %t.result -avoid-tool-args
6+
// RUN: diff -u %S/Outputs/empty-baseline.json %t.result
7+
// RUN: %api-digester -deserialize-sdk -input-paths %S/Outputs/empty-baseline.json -o %t.result
8+
// RUN: diff -u %S/Outputs/empty-baseline.json %t.result

tools/swift-api-digester/ModuleAnalyzerNodes.cpp

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2070,16 +2070,24 @@ void SwiftDeclCollector::serialize(StringRef Filename) {
20702070
SwiftDeclCollector::serialize(Filename, RootNode);
20712071
}
20722072

2073+
SDKNodeRoot *
2074+
swift::ide::api::getEmptySDKNodeRoot(SDKContext &SDKCtx) {
2075+
SwiftDeclCollector Collector(SDKCtx);
2076+
return Collector.getSDKRoot();
2077+
}
2078+
20732079
SDKNodeRoot*
20742080
swift::ide::api::getSDKNodeRoot(SDKContext &SDKCtx,
20752081
const CompilerInvocation &InitInvok,
2076-
const llvm::StringSet<> &ModuleNames,
2077-
CheckerOptions Opts) {
2082+
const llvm::StringSet<> &ModuleNames) {
2083+
CheckerOptions Opts = SDKCtx.getOpts();
20782084
CompilerInvocation Invocation(InitInvok);
20792085

20802086
CompilerInstance &CI = SDKCtx.newCompilerInstance();
20812087
// Display diagnostics to stderr.
2082-
PrintingDiagnosticConsumer PrintDiags;
2088+
PrintingDiagnosticConsumer PrintDiags(llvm::errs());
2089+
if (llvm::errs().has_colors())
2090+
PrintDiags.forceColors();
20832091
CI.addDiagnosticConsumer(&PrintDiags);
20842092
if (CI.setup(Invocation)) {
20852093
llvm::errs() << "Failed to setup the compiler instance\n";
@@ -2102,7 +2110,7 @@ swift::ide::api::getSDKNodeRoot(SDKContext &SDKCtx,
21022110
if (Opts.Verbose)
21032111
llvm::errs() << "Loading module: " << Name << "...\n";
21042112
auto *M = Ctx.getModuleByName(Name);
2105-
if (!M || M->failedToLoad()) {
2113+
if (!M || M->failedToLoad() || Ctx.Diags.hadAnyError()) {
21062114
llvm::errs() << "Failed to load module: " << Name << '\n';
21072115
if (Opts.AbortOnModuleLoadFailure)
21082116
return nullptr;
@@ -2118,18 +2126,24 @@ swift::ide::api::getSDKNodeRoot(SDKContext &SDKCtx,
21182126
return Collector.getSDKRoot();
21192127
}
21202128

2129+
void swift::ide::api::dumpSDKRoot(SDKNodeRoot *Root, StringRef OutputFile) {
2130+
assert(Root);
2131+
auto Opts = Root->getSDKContext().getOpts();
2132+
if (Opts.Verbose)
2133+
llvm::errs() << "Dumping SDK...\n";
2134+
SwiftDeclCollector::serialize(OutputFile, Root);
2135+
if (Opts.Verbose)
2136+
llvm::errs() << "Dumped to "<< OutputFile << "\n";
2137+
}
2138+
21212139
int swift::ide::api::dumpSDKContent(const CompilerInvocation &InitInvok,
21222140
const llvm::StringSet<> &ModuleNames,
21232141
StringRef OutputFile, CheckerOptions Opts) {
21242142
SDKContext SDKCtx(Opts);
2125-
SDKNode *Root = getSDKNodeRoot(SDKCtx, InitInvok, ModuleNames, Opts);
2143+
SDKNodeRoot *Root = getSDKNodeRoot(SDKCtx, InitInvok, ModuleNames);
21262144
if (!Root)
21272145
return 1;
2128-
if (Opts.Verbose)
2129-
llvm::errs() << "Dumping SDK...\n";
2130-
SwiftDeclCollector::serialize(OutputFile, Root);
2131-
if (Opts.Verbose)
2132-
llvm::errs() << "Dumped to "<< OutputFile << "\n";
2146+
dumpSDKRoot(Root, OutputFile);
21332147
return 0;
21342148
}
21352149

tools/swift-api-digester/ModuleAnalyzerNodes.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -748,8 +748,11 @@ int dumpSwiftModules(const CompilerInvocation &InitInvok,
748748

749749
SDKNodeRoot *getSDKNodeRoot(SDKContext &SDKCtx,
750750
const CompilerInvocation &InitInvok,
751-
const llvm::StringSet<> &ModuleNames,
752-
CheckerOptions Opts);
751+
const llvm::StringSet<> &ModuleNames);
752+
753+
SDKNodeRoot *getEmptySDKNodeRoot(SDKContext &SDKCtx);
754+
755+
void dumpSDKRoot(SDKNodeRoot *Root, StringRef OutputFile);
753756

754757
int dumpSDKContent(const CompilerInvocation &InitInvok,
755758
const llvm::StringSet<> &ModuleNames,

tools/swift-api-digester/swift-api-digester.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ namespace {
4848
DeserializeSDK,
4949
GenerateNameCorrectionTemplate,
5050
FindUsr,
51+
GenerateEmptyBaseline,
5152
};
5253
} // end anonymous namespace
5354

@@ -173,7 +174,10 @@ Action(llvm::cl::desc("Mode:"), llvm::cl::init(ActionType::None),
173174
"Find USR for decls by given condition"),
174175
clEnumValN(ActionType::GenerateNameCorrectionTemplate,
175176
"generate-name-correction",
176-
"Generate name correction template")));
177+
"Generate name correction template"),
178+
clEnumValN(ActionType::GenerateEmptyBaseline,
179+
"generate-empty-baseline",
180+
"Generate an empty baseline")));
177181

178182
static llvm::cl::list<std::string>
179183
SDKJsonPaths("input-paths",
@@ -2480,13 +2484,12 @@ static CheckerOptions getCheckOpts(int argc, char *argv[]) {
24802484
return Opts;
24812485
}
24822486

2483-
static SDKNodeRoot *getSDKRoot(const char *Main, SDKContext &Ctx,
2484-
CheckerOptions Opts, bool IsBaseline) {
2487+
static SDKNodeRoot *getSDKRoot(const char *Main, SDKContext &Ctx, bool IsBaseline) {
24852488
CompilerInvocation Invok;
24862489
llvm::StringSet<> Modules;
24872490
if (prepareForDump(Main, Invok, Modules, IsBaseline))
24882491
return nullptr;
2489-
return getSDKNodeRoot(Ctx, Invok, Modules, Opts);
2492+
return getSDKNodeRoot(Ctx, Invok, Modules);
24902493
}
24912494

24922495
static bool hasBaselineInput() {
@@ -2537,8 +2540,8 @@ int main(int argc, char *argv[]) {
25372540
std::move(protocolWhitelist));
25382541
else {
25392542
SDKContext Ctx(Opts);
2540-
return diagnoseModuleChange(Ctx, getSDKRoot(argv[0], Ctx, Opts, true),
2541-
getSDKRoot(argv[0], Ctx, Opts, false),
2543+
return diagnoseModuleChange(Ctx, getSDKRoot(argv[0], Ctx, true),
2544+
getSDKRoot(argv[0], Ctx, false),
25422545
options::OutputFile,
25432546
std::move(protocolWhitelist));
25442547
}
@@ -2567,6 +2570,11 @@ int main(int argc, char *argv[]) {
25672570
Store.addStorePath(Paths[I]);
25682571
return deserializeNameCorrection(Store, options::OutputFile);
25692572
}
2573+
case ActionType::GenerateEmptyBaseline: {
2574+
SDKContext Ctx(Opts);
2575+
dumpSDKRoot(getEmptySDKNodeRoot(Ctx), options::OutputFile);
2576+
return 0;
2577+
}
25702578
case ActionType::FindUsr: {
25712579
if (options::SDKJsonPaths.size() != 1) {
25722580
llvm::cl::PrintHelpMessage();

utils/api_checker/swift-api-checker.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,13 @@ def escapeCmdArg(arg):
3333
def check_call(cmd, cwd=None, env=os.environ, verbose=False, output=None):
3434
if verbose:
3535
print(' '.join([escapeCmdArg(arg) for arg in cmd]))
36-
return subprocess.check_call(cmd, cwd=cwd, env=env,
37-
stderr=None, stdout=output)
36+
try:
37+
subprocess.check_call(cmd, cwd=cwd, env=env,
38+
stderr=None, stdout=output)
39+
return 0
40+
except Exception as error:
41+
printerr(error)
42+
return 1
3843

3944

4045
def check_output(cmd, verbose=False):

0 commit comments

Comments
 (0)