Browse Source

Improve the error handling of the Parse method so we can correctly diagnose the issue in the driver.

pull/1/head
triton 13 years ago
parent
commit
7da24a02f6
  1. 15
      src/Generator/Driver.cs
  2. 16
      src/Parser/Parser.cpp
  3. 9
      src/Parser/Parser.h

15
src/Generator/Driver.cs

@ -55,8 +55,19 @@ namespace Cxxi
var parser = new Parser(Options); var parser = new Parser(Options);
parser.HeaderParsed += (file, result) => parser.HeaderParsed += (file, result) =>
Console.WriteLine(result.Success ? " Parsed '" + file + "'." : {
" Could not parse '" + file + "'."); switch (result.Kind)
{
case ParserResultKind.Success:
Console.WriteLine(" Parsed '{0}'", file);
break;
case ParserResultKind.Error:
Console.WriteLine(" Error parsing '{0}'", file);
break;
case ParserResultKind.FileNotFound:
Console.WriteLine(" File '{0}' was not found", file);
break;
}
parser.ParseHeaders(Options.Headers); parser.ParseHeaders(Options.Headers);
Library = parser.Library; Library = parser.Library;

16
src/Parser/Parser.cpp

@ -13,6 +13,7 @@
#include <clang/Config/config.h> #include <clang/Config/config.h>
#include <clang/AST/ASTContext.h> #include <clang/AST/ASTContext.h>
#include <clang/AST/DeclTemplate.h> #include <clang/AST/DeclTemplate.h>
#include <clang/Lex/DirectoryLookup.h>
#include <clang/Lex/HeaderSearch.h> #include <clang/Lex/HeaderSearch.h>
#include <clang/Lex/PreprocessingRecord.h> #include <clang/Lex/PreprocessingRecord.h>
#include <clang/Frontend/Utils.h> #include <clang/Frontend/Utils.h>
@ -1429,7 +1430,7 @@ ParserResult^ Parser::Parse(const std::string& File)
if (File.empty()) if (File.empty())
{ {
res->Success = false; res->Kind = ParserResultKind::FileNotFound;
return res; return res;
} }
@ -1438,6 +1439,15 @@ ParserResult^ Parser::Parse(const std::string& File)
auto DiagClient = new DiagnosticConsumer(); auto DiagClient = new DiagnosticConsumer();
C->getDiagnostics().setClient(DiagClient); C->getDiagnostics().setClient(DiagClient);
// Check that the file is reachable.
const clang::DirectoryLookup *Dir;
if (!C->getPreprocessor().getHeaderSearchInfo().LookupFile(File, /*isAngled*/true,
nullptr, Dir, nullptr, nullptr, nullptr, nullptr))
{
res->Kind = ParserResultKind::FileNotFound;
return res;
}
// Create a virtual file that includes the header. This gets rid of some // Create a virtual file that includes the header. This gets rid of some
// Clang warnings about parsing an header file as the main file. // Clang warnings about parsing an header file as the main file.
@ -1471,13 +1481,13 @@ ParserResult^ Parser::Parse(const std::string& File)
if(DiagClient->getNumErrors() != 0) if(DiagClient->getNumErrors() != 0)
{ {
res->Success = false; res->Kind = ParserResultKind::Error;
return res; return res;
} }
AST = &C->getASTContext(); AST = &C->getASTContext();
WalkAST(); WalkAST();
res->Success = true; res->Kind = ParserResultKind::Success;
return res; return res;
} }

9
src/Parser/Parser.h

@ -61,6 +61,13 @@ public value struct ParserDiagnostic
System::String^ Message; System::String^ Message;
}; };
public enum struct ParserResultKind
{
Success,
Error,
FileNotFound
};
public ref struct ParserResult public ref struct ParserResult
{ {
ParserResult() ParserResult()
@ -68,7 +75,7 @@ public ref struct ParserResult
Diagnostics = gcnew List<ParserDiagnostic>(); Diagnostics = gcnew List<ParserDiagnostic>();
} }
bool Success; ParserResultKind Kind;
Cxxi::Library^ Library; Cxxi::Library^ Library;
List<ParserDiagnostic>^ Diagnostics; List<ParserDiagnostic>^ Diagnostics;
}; };

Loading…
Cancel
Save