Skip to content

Commit e464da4

Browse files
authored
Bug fixes for GCC 11, cache conversion robustness and error handling (#1765)
* Fix missing Attribute dtor in GCC 11. Parser bindings are expecting this, yet this was optimized out under GCC 11. * Improve error handling for failed library parsing. * Make the converted declaration cache more robust. We were getting a failure due to duplicated original pointers. Make it take the declaration kind into account as a key to the cache. * Change ConsoleDriver.Run to return a failure bool.
1 parent 25d6325 commit e464da4

File tree

6 files changed

+45
-20
lines changed

6 files changed

+45
-20
lines changed

src/CppParser/AST.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,6 +1078,8 @@ HTMLStartTagComment::Attribute::Attribute() {}
10781078

10791079
HTMLStartTagComment::Attribute::Attribute(const Attribute& rhs) : name(rhs.name), value(rhs.value) {}
10801080

1081+
HTMLStartTagComment::Attribute::~Attribute() {}
1082+
10811083
HTMLStartTagComment::HTMLStartTagComment() : HTMLTagComment(CommentKind::HTMLStartTagComment) {}
10821084

10831085
DEF_VECTOR(HTMLStartTagComment, HTMLStartTagComment::Attribute, Attributes)

src/CppParser/AST.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ class CS_API HTMLStartTagComment : public HTMLTagComment
201201
public:
202202
Attribute();
203203
Attribute(const Attribute&);
204+
~Attribute();
204205
std::string name;
205206
std::string value;
206207
};

src/CppParser/CppParser.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ struct CS_API ParserDiagnostic
8282
~ParserDiagnostic();
8383
std::string fileName;
8484
std::string message;
85-
ParserDiagnosticLevel level;
86-
int lineNumber;
87-
int columnNumber;
85+
ParserDiagnosticLevel level { ParserDiagnosticLevel::Ignored };
86+
int lineNumber {0};
87+
int columnNumber {0};
8888
};
8989

9090
enum class ParserResultKind

src/CppParser/Parser.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4713,7 +4713,13 @@ ParserResult* Parser::ParseLibrary(const CppLinkerOptions* Opts)
47134713
auto BinaryOrErr = llvm::object::createBinary(FileEntry);
47144714
if (!BinaryOrErr)
47154715
{
4716-
auto Error = BinaryOrErr.takeError();
4716+
auto ErrMsg = llvm::toString(BinaryOrErr.takeError());
4717+
auto Diag = ParserDiagnostic();
4718+
Diag.fileName = FileEntry;
4719+
Diag.message = ErrMsg;
4720+
Diag.level = ParserDiagnosticLevel::Error;
4721+
res->Diagnostics.push_back(Diag);
4722+
47174723
res->kind = ParserResultKind::Error;
47184724
return res;
47194725
}

src/Generator/Driver.cs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,17 @@ void OnFileParsed(IEnumerable<string> files, ParserResult result)
137137
if (diag.Level == ParserDiagnosticLevel.Note)
138138
continue;
139139

140-
Diagnostics.Message("{0}({1},{2}): {3}: {4}",
141-
diag.FileName, diag.LineNumber, diag.ColumnNumber,
142-
diag.Level.ToString().ToLower(), diag.Message);
140+
if (diag.LineNumber == 0 && diag.ColumnNumber == 0)
141+
{
142+
Diagnostics.Message("{0}: {1}: {2}",
143+
diag.FileName, diag.Level.ToString().ToLower(), diag.Message);
144+
}
145+
else
146+
{
147+
Diagnostics.Message("{0}({1},{2}): {3}: {4}",
148+
diag.FileName, diag.LineNumber, diag.ColumnNumber,
149+
diag.Level.ToString().ToLower(), diag.Message);
150+
}
143151
}
144152
}
145153

@@ -196,7 +204,10 @@ public bool ParseLibraries()
196204

197205
using var res = ClangParser.ParseLibrary(linkerOptions);
198206
if (res.Kind != ParserResultKind.Success)
207+
{
208+
res.Dispose();
199209
continue;
210+
}
200211

201212
for (uint i = 0; i < res.LibrariesCount; i++)
202213
Context.Symbols.Libraries.Add(ClangParser.ConvertLibrary(res.GetLibraries(i)));
@@ -206,7 +217,7 @@ public bool ParseLibraries()
206217
Context.Symbols.IndexSymbols();
207218
SortModulesByDependencies();
208219

209-
return true;
220+
return !hasParsingErrors;
210221
}
211222

212223
public void SetupPasses(ILibrary library)
@@ -412,7 +423,7 @@ public void Dispose()
412423

413424
public static class ConsoleDriver
414425
{
415-
public static void Run(ILibrary library)
426+
public static bool Run(ILibrary library)
416427
{
417428
var options = new DriverOptions();
418429
using var driver = new Driver(options);
@@ -427,15 +438,15 @@ public static void Run(ILibrary library)
427438
Diagnostics.Message("Parsing libraries...");
428439

429440
if (!driver.ParseLibraries())
430-
return;
441+
return false;
431442

432443
if (!options.Quiet)
433444
Diagnostics.Message("Parsing code...");
434445

435446
if (!driver.ParseCode())
436447
{
437448
Diagnostics.Error("CppSharp has encountered an error while parsing code.");
438-
return;
449+
return false;
439450
}
440451

441452
new CleanUnitPass { Context = driver.Context }.VisitASTContext(driver.Context.ASTContext);
@@ -462,7 +473,7 @@ public static void Run(ILibrary library)
462473
Diagnostics.Message("Generating code...");
463474

464475
if (options.DryRun)
465-
return;
476+
return true;
466477

467478
var outputs = driver.GenerateCode();
468479

@@ -477,6 +488,8 @@ public static void Run(ILibrary library)
477488
driver.SaveCode(outputs);
478489
if (driver.Options.IsCSharpGenerator && driver.Options.CompileCode)
479490
driver.Options.Modules.Any(m => !driver.CompileCode(m));
491+
492+
return true;
480493
}
481494
}
482495
}

src/Parser/ASTConverter.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -847,7 +847,7 @@ public unsafe class DeclConverter : DeclVisitor<AST.Declaration>
847847
readonly CommentConverter commentConverter;
848848
readonly StmtConverter stmtConverter;
849849

850-
readonly Dictionary<IntPtr, AST.Declaration> Declarations;
850+
readonly Dictionary<(IntPtr, DeclarationKind), AST.Declaration> Declarations;
851851
readonly Dictionary<IntPtr, AST.PreprocessedEntity> PreprocessedEntities;
852852
readonly Dictionary<IntPtr, AST.FunctionTemplateSpecialization> FunctionTemplateSpecializations;
853853

@@ -857,7 +857,7 @@ public DeclConverter(TypeConverter type, CommentConverter comment, StmtConverter
857857
typeConverter = type;
858858
commentConverter = comment;
859859
stmtConverter = stmt;
860-
Declarations = new Dictionary<IntPtr, AST.Declaration>();
860+
Declarations = new Dictionary<(IntPtr, DeclarationKind), AST.Declaration>();
861861
PreprocessedEntities = new Dictionary<IntPtr, AST.PreprocessedEntity>();
862862
FunctionTemplateSpecializations = new Dictionary<IntPtr, AST.FunctionTemplateSpecialization>();
863863
}
@@ -876,8 +876,9 @@ public override AST.Declaration Visit(Declaration decl)
876876

877877
// Check if the declaration was already handled and return its
878878
// existing instance.
879-
if (CheckForDuplicates(decl) && Declarations.ContainsKey(originalPtr))
880-
return Declarations[originalPtr];
879+
var key = (decl.OriginalPtr, decl.Kind);
880+
if (CheckForDuplicates(decl) && Declarations.TryGetValue(key, out var visit))
881+
return visit;
881882

882883
return base.Visit(decl);
883884
}
@@ -981,15 +982,17 @@ bool CheckForDuplicates(Declaration decl)
981982

982983
void VisitDeclaration(Declaration decl, AST.Declaration _decl)
983984
{
984-
var originalPtr = decl.OriginalPtr;
985+
var key = (decl.OriginalPtr, decl.Kind);
985986

986987
if (CheckForDuplicates(decl))
987-
if (Declarations.ContainsKey(originalPtr))
988+
{
989+
if (Declarations.ContainsKey(key))
988990
throw new NotSupportedException("Duplicate declaration processed");
991+
}
989992

990993
// Add the declaration to the map so that we can check if have
991994
// already handled it and return the declaration.
992-
Declarations[originalPtr] = _decl;
995+
Declarations[key] = _decl;
993996

994997
_decl.Access = VisitAccessSpecifier(decl.Access);
995998
_decl.Name = decl.Name;
@@ -1020,7 +1023,7 @@ void VisitDeclaration(Declaration decl, AST.Declaration _decl)
10201023
_decl.PreprocessedEntities.Add(_entity);
10211024
}
10221025

1023-
_decl.OriginalPtr = originalPtr;
1026+
_decl.OriginalPtr = decl.OriginalPtr;
10241027

10251028
NativeObjects.Add(decl);
10261029

0 commit comments

Comments
 (0)