Browse Source

Extract diagnostic handling to a method.

pull/47/merge
triton 12 years ago
parent
commit
5ea71589a8
  1. 99
      src/Parser/Parser.cpp
  2. 2
      src/Parser/Parser.h

99
src/Parser/Parser.cpp

@ -2073,56 +2073,12 @@ struct DiagnosticConsumer : public clang::DiagnosticConsumer
std::vector<Diagnostic> Diagnostics; std::vector<Diagnostic> Diagnostics;
}; };
ParserResult^ Parser::ParseHeader(const std::string& File) void Parser::HandleDiagnostics(ParserResult^ res)
{
auto res = gcnew ParserResult();
res->Library = Lib;
if (File.empty())
{
res->Kind = ParserResultKind::FileNotFound;
return res;
}
SetupHeader();
auto SC = new clang::SemaConsumer();
C->setASTConsumer(SC);
C->createSema(clang::TU_Complete, 0);
SC->InitializeSema(C->getSema());
auto DiagClient = new DiagnosticConsumer();
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; auto DiagClient = (DiagnosticConsumer&) C->getDiagnosticClient();
return res;
}
// Create a virtual file that includes the header. This gets rid of some
// Clang warnings about parsing an header file as the main file.
std::string str;
str += "#include \"" + File + "\"" + "\n";
str += "\0";
auto buffer = llvm::MemoryBuffer::getMemBuffer(str);
C->getSourceManager().createMainFileIDForMemBuffer(buffer);
clang::DiagnosticConsumer* client = C->getDiagnostics().getClient();
client->BeginSourceFile(C->getLangOpts(), &C->getPreprocessor());
ParseAST(C->getSema(), /*PrintStats=*/false, /*SkipFunctionBodies=*/true);
client->EndSourceFile();
// Convert the diagnostics to the managed types // Convert the diagnostics to the managed types
for each (auto& Diag in DiagClient->Diagnostics) for each (auto& Diag in DiagClient.Diagnostics)
{ {
using namespace clix; using namespace clix;
@ -2168,6 +2124,55 @@ ParserResult^ Parser::ParseHeader(const std::string& File)
res->Diagnostics->Add(PDiag); res->Diagnostics->Add(PDiag);
} }
}
ParserResult^ Parser::ParseHeader(const std::string& File)
{
auto res = gcnew ParserResult();
res->Library = Lib;
if (File.empty())
{
res->Kind = ParserResultKind::FileNotFound;
return res;
}
SetupHeader();
auto SC = new clang::SemaConsumer();
C->setASTConsumer(SC);
C->createSema(clang::TU_Complete, 0);
SC->InitializeSema(C->getSema());
auto DiagClient = new DiagnosticConsumer();
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
// Clang warnings about parsing an header file as the main file.
std::string str;
str += "#include \"" + File + "\"" + "\n";
str += "\0";
auto buffer = llvm::MemoryBuffer::getMemBuffer(str);
C->getSourceManager().createMainFileIDForMemBuffer(buffer);
clang::DiagnosticConsumer* client = C->getDiagnostics().getClient();
client->BeginSourceFile(C->getLangOpts(), &C->getPreprocessor());
ParseAST(C->getSema(), /*PrintStats=*/false, /*SkipFunctionBodies=*/true);
client->EndSourceFile();
if(C->getDiagnosticClient().getNumErrors() != 0) if(C->getDiagnosticClient().getNumErrors() != 0)
{ {

2
src/Parser/Parser.h

@ -177,6 +177,8 @@ protected:
clang::CallingConv GetAbiCallConv(clang::CallingConv CC, clang::CallingConv GetAbiCallConv(clang::CallingConv CC,
bool IsInstMethod, bool IsVariadic); bool IsInstMethod, bool IsVariadic);
void HandleDiagnostics(ParserResult^ res);
int Index; int Index;
gcroot<CppSharp::AST::Library^> Lib; gcroot<CppSharp::AST::Library^> Lib;
gcroot<ParserOptions^> Opts; gcroot<ParserOptions^> Opts;

Loading…
Cancel
Save