Skip to content

Commit 73ab168

Browse files
authored
Merge pull request #67833 from hjyamauchi/lldblog
[lldb] Capture error messages from parseASTSection to log from caller
2 parents 933fa26 + 24dfc90 commit 73ab168

File tree

6 files changed

+126
-19
lines changed

6 files changed

+126
-19
lines changed

include/swift/ASTSectionImporter/ASTSectionImporter.h

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#define SWIFT_ASTSECTION_IMPORTER_H
1919

2020
#include "swift/Basic/LLVM.h"
21+
#include "swift/Serialization/Validation.h"
22+
#include "llvm/Support/Error.h"
2123
#include <string>
2224

2325
namespace llvm {
@@ -26,15 +28,47 @@ class Triple;
2628
namespace swift {
2729
class MemoryBufferSerializedModuleLoader;
2830

31+
class ASTSectionParseError : public llvm::ErrorInfo<ASTSectionParseError> {
32+
public:
33+
static char ID;
34+
35+
serialization::Status Error;
36+
std::string ErrorMessage;
37+
38+
ASTSectionParseError(serialization::Status Error,
39+
StringRef ErrorMessage = {})
40+
: Error(Error), ErrorMessage(ErrorMessage) {
41+
assert(Error != serialization::Status::Valid);
42+
}
43+
ASTSectionParseError(const ASTSectionParseError &Other)
44+
: ASTSectionParseError(Other.Error, Other.ErrorMessage) {}
45+
ASTSectionParseError &operator=(const ASTSectionParseError &Other) {
46+
Error = Other.Error;
47+
ErrorMessage = Other.ErrorMessage;
48+
return *this;
49+
}
50+
51+
std::string toString() const;
52+
void log(llvm::raw_ostream &OS) const override;
53+
std::error_code convertToErrorCode() const override;
54+
};
55+
2956
/// Provided a memory buffer with an entire Mach-O __swift_ast section, this
3057
/// function makes memory buffer copies of all swift modules found in it and
3158
/// registers them using registerMemoryBuffer() so they can be found by
32-
/// loadModule(). The access path of all modules found in the section is
33-
/// appended to the vector foundModules.
59+
/// loadModule().
3460
/// \param filter If fully specified, only matching modules are registered.
35-
/// \return true if successful.
61+
/// \return a vector of the access path of all modules found in the
62+
/// section if successful.
63+
llvm::Expected<SmallVector<std::string, 4>>
64+
parseASTSection(MemoryBufferSerializedModuleLoader &Loader,
65+
StringRef Data, const llvm::Triple &filter);
66+
67+
// An old version temporarily left for remaining call site.
68+
// TODO: remove this once the other version is committed and used.
3669
bool parseASTSection(MemoryBufferSerializedModuleLoader &Loader,
3770
StringRef Data, const llvm::Triple &filter,
3871
SmallVectorImpl<std::string> &foundModules);
72+
3973
}
4074
#endif

include/swift/Serialization/SerializationOptions.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
#include "swift/Basic/PathRemapper.h"
1919
#include "llvm/Support/VersionTuple.h"
2020

21+
#include <set>
22+
#include <string>
23+
#include <vector>
24+
2125
namespace swift {
2226

2327
class SerializationOptions {

include/swift/Serialization/Validation.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
#ifndef SWIFT_SERIALIZATION_VALIDATION_H
1414
#define SWIFT_SERIALIZATION_VALIDATION_H
1515

16+
#include "swift/AST/Identifier.h"
1617
#include "swift/Basic/LLVM.h"
18+
#include "swift/Basic/Version.h"
1719
#include "swift/Serialization/SerializationOptions.h"
1820
#include "llvm/ADT/ArrayRef.h"
1921
#include "llvm/ADT/SmallVector.h"
@@ -83,6 +85,9 @@ enum class Status {
8385
SDKMismatch
8486
};
8587

88+
/// Returns the string for the Status enum.
89+
std::string StatusToString(Status S);
90+
8691
/// Returns true if the data looks like it contains a serialized AST.
8792
bool isSerializedAST(StringRef data);
8893

lib/ASTSectionImporter/ASTSectionImporter.cpp

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,32 @@
2626

2727
using namespace swift;
2828

29-
bool swift::parseASTSection(MemoryBufferSerializedModuleLoader &Loader,
30-
StringRef buf,
31-
const llvm::Triple &filter,
32-
SmallVectorImpl<std::string> &foundModules) {
29+
std::string ASTSectionParseError::toString() const {
30+
std::string S;
31+
llvm::raw_string_ostream SS(S);
32+
SS << serialization::StatusToString(Error);
33+
if (!ErrorMessage.empty())
34+
SS << ": " << ErrorMessage;
35+
return SS.str();
36+
}
37+
38+
void ASTSectionParseError::log(raw_ostream &OS) const { OS << toString(); }
39+
40+
std::error_code ASTSectionParseError::convertToErrorCode() const {
41+
llvm_unreachable("Function not implemented.");
42+
}
43+
44+
char ASTSectionParseError::ID;
45+
46+
llvm::Expected<SmallVector<std::string, 4>>
47+
swift::parseASTSection(MemoryBufferSerializedModuleLoader &Loader,
48+
StringRef buf,
49+
const llvm::Triple &filter) {
3350
if (!serialization::isSerializedAST(buf))
34-
return false;
51+
return llvm::make_error<ASTSectionParseError>(
52+
serialization::Status::Malformed);
3553

54+
SmallVector<std::string, 4> foundModules;
3655
bool haveFilter = filter.getOS() != llvm::Triple::UnknownOS &&
3756
filter.getArch() != llvm::Triple::UnknownArch;
3857
// An AST section consists of one or more AST modules, optionally with
@@ -44,6 +63,8 @@ bool swift::parseASTSection(MemoryBufferSerializedModuleLoader &Loader,
4463

4564
assert(info.name.size() < (2 << 10) && "name failed sanity check");
4665

66+
std::string error;
67+
llvm::raw_string_ostream errs(error);
4768
if (info.status == serialization::Status::Valid) {
4869
assert(info.bytes != 0);
4970
bool selected = true;
@@ -62,23 +83,38 @@ bool swift::parseASTSection(MemoryBufferSerializedModuleLoader &Loader,
6283
foundModules.push_back(info.name.str());
6384
}
6485
} else {
65-
llvm::dbgs() << "Unable to load module";
86+
errs << "Unable to load module";
6687
if (!info.name.empty())
67-
llvm::dbgs() << " '" << info.name << '\'';
68-
llvm::dbgs() << ".\n";
88+
errs << " '" << info.name << '\'';
89+
errs << ".";
6990
}
7091

7192
if (info.bytes == 0)
72-
return false;
93+
return llvm::make_error<ASTSectionParseError>(info.status, errs.str());
7394

7495
if (info.bytes > buf.size()) {
75-
llvm::dbgs() << "AST section too small.\n";
76-
return false;
96+
errs << "AST section too small.";
97+
return llvm::make_error<ASTSectionParseError>(
98+
serialization::Status::Malformed, errs.str());
7799
}
78100

79101
buf = buf.substr(
80102
llvm::alignTo(info.bytes, swift::serialization::SWIFTMODULE_ALIGNMENT));
81103
}
82104

105+
return foundModules;
106+
}
107+
108+
bool swift::parseASTSection(MemoryBufferSerializedModuleLoader &Loader,
109+
StringRef buf,
110+
const llvm::Triple &filter,
111+
SmallVectorImpl<std::string> &foundModules) {
112+
auto Result = parseASTSection(Loader, buf, filter);
113+
if (auto E = Result.takeError()) {
114+
llvm::dbgs() << toString(std::move(E));
115+
return false;
116+
}
117+
for (auto m : *Result)
118+
foundModules.push_back(m);
83119
return true;
84120
}

lib/Serialization/ModuleFileSharedCore.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,28 @@ static bool validateInputBlock(
514514
return false;
515515
}
516516

517+
std::string serialization::StatusToString(Status S) {
518+
switch (S) {
519+
case Status::Valid: return "Valid";
520+
case Status::FormatTooOld: return "FormatTooOld";
521+
case Status::FormatTooNew: return "FormatTooNew";
522+
case Status::RevisionIncompatible: return "RevisionIncompatible";
523+
case Status::NotInOSSA: return "NotInOSSA";
524+
case Status::MissingDependency: return "MissingDependency";
525+
case Status::MissingUnderlyingModule: return "MissingUnderlyingModule";
526+
case Status::CircularDependency: return "CircularDependency";
527+
case Status::FailedToLoadBridgingHeader: return "FailedToLoadBridgingHeader";
528+
case Status::Malformed: return "Malformed";
529+
case Status::MalformedDocumentation: return "MalformedDocumentation";
530+
case Status::NameMismatch: return "NameMismatch";
531+
case Status::TargetIncompatible: return "TargetIncompatible";
532+
case Status::TargetTooNew: return "TargetTooNew";
533+
case Status::SDKMismatch: return "SDKMismatch";
534+
default:
535+
llvm_unreachable("The switch should cover all cases");
536+
}
537+
}
538+
517539
bool serialization::isSerializedAST(StringRef data) {
518540
StringRef signatureStr(reinterpret_cast<const char *>(SWIFTMODULE_SIGNATURE),
519541
std::size(SWIFTMODULE_SIGNATURE));

tools/lldb-moduleimport-test/lldb-moduleimport-test.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -364,14 +364,20 @@ int main(int argc, char **argv) {
364364
ClangImporter->setDWARFImporterDelegate(dummyDWARFImporter);
365365
}
366366

367+
llvm::SmallString<0> error;
368+
llvm::raw_svector_ostream errs(error);
367369
llvm::Triple filter(Filter);
368-
for (auto &Module : Modules)
369-
if (!parseASTSection(*CI.getMemoryBufferSerializedModuleLoader(),
370-
StringRef(Module.first, Module.second), filter,
371-
modules)) {
372-
llvm::errs() << "error: Failed to parse AST section!\n";
370+
for (auto &Module : Modules) {
371+
auto Result = parseASTSection(
372+
*CI.getMemoryBufferSerializedModuleLoader(),
373+
StringRef(Module.first, Module.second), filter);
374+
if (auto E = Result.takeError()) {
375+
std::string error = toString(std::move(E));
376+
llvm::errs() << "error: Failed to parse AST section! " << error << "\n";
373377
return 1;
374378
}
379+
modules.insert(modules.end(), Result->begin(), Result->end());
380+
}
375381

376382
// Attempt to import all modules we found.
377383
for (auto path : modules) {

0 commit comments

Comments
 (0)