Browse Source

Fixed regression in parsing of preprocessed entities.

Specifically we now store the pointer of the original declaration and check for duplicates.
pull/137/merge
triton 12 years ago
parent
commit
677ca81154
  1. 72
      src/CppParser/Parser.cpp
  2. 2
      src/CppParser/Parser.h
  3. 68
      src/Parser/Parser.cpp
  4. 2
      src/Parser/Parser.h

72
src/CppParser/Parser.cpp

@ -1915,58 +1915,80 @@ bool Parser::GetDeclText(clang::SourceRange SR, std::string& Text)
return !Invalid && !Text.empty(); return !Invalid && !Text.empty();
} }
void Parser::HandlePreprocessedEntities(Declaration* Decl, PreprocessedEntity* Parser::WalkPreprocessedEntity(
clang::SourceRange sourceRange, Declaration* Decl, clang::PreprocessedEntity* PPEntity)
MacroLocation macroLocation)
{ {
if (sourceRange.isInvalid()) return;
auto& SourceMgr = C->getSourceManager();
auto isBefore = SourceMgr.isBeforeInTranslationUnit(sourceRange.getEnd(),
sourceRange.getBegin());
if (isBefore) return;
assert(!SourceMgr.isBeforeInTranslationUnit(sourceRange.getEnd(),
sourceRange.getBegin()));
using namespace clang; using namespace clang;
auto PPRecord = C->getPreprocessor().getPreprocessingRecord();
auto Range = PPRecord->getPreprocessedEntitiesInRange(sourceRange);
for (auto it = Range.first; it != Range.second; ++it) for (unsigned I = 0, E = Decl->PreprocessedEntities.size();
I != E; ++I)
{ {
auto PPEntity = (*it); auto Entity = Decl->PreprocessedEntities[I];
if (Entity->OriginalPtr == PPEntity)
return Entity;
}
PreprocessedEntity* Entity; PreprocessedEntity* Entity;
switch(PPEntity->getKind()) switch(PPEntity->getKind())
{ {
case clang::PreprocessedEntity::MacroExpansionKind: case clang::PreprocessedEntity::MacroExpansionKind:
{ {
auto MD = cast<clang::MacroExpansion>(PPEntity); const MacroExpansion* MD = cast<MacroExpansion>(PPEntity);
Entity = new MacroExpansion(); Entity = new MacroExpansion();
std::string Text; std::string Text;
if (!GetDeclText(PPEntity->getSourceRange(), Text)) if (!GetDeclText(PPEntity->getSourceRange(), Text))
continue; return nullptr;
static_cast<MacroExpansion*>(Entity)->Text = Text; static_cast<MacroExpansion*>(Entity)->Text = Text;
break; break;
} }
case clang::PreprocessedEntity::MacroDefinitionKind: case clang::PreprocessedEntity::MacroDefinitionKind:
{ {
auto MD = cast<clang::MacroDefinition>(PPEntity); const MacroDefinition* MD = cast<MacroDefinition>(PPEntity);
Entity = new MacroDefinition(); Entity = new MacroDefinition();
break; break;
} }
default: default:
continue; return nullptr;
} }
Entity->Location = macroLocation; Entity->OriginalPtr = PPEntity;
Decl->PreprocessedEntities.push_back(Entity); Decl->PreprocessedEntities.push_back(Entity);
return Entity;
}
void Parser::HandlePreprocessedEntities(Declaration* Decl,
clang::SourceRange sourceRange,
MacroLocation macroLocation)
{
if (sourceRange.isInvalid()) return;
auto& SourceMgr = C->getSourceManager();
auto isBefore = SourceMgr.isBeforeInTranslationUnit(sourceRange.getEnd(),
sourceRange.getBegin());
if (isBefore) return;
assert(!SourceMgr.isBeforeInTranslationUnit(sourceRange.getEnd(),
sourceRange.getBegin()));
using namespace clang;
auto PPRecord = C->getPreprocessor().getPreprocessingRecord();
auto Range = PPRecord->getPreprocessedEntitiesInRange(sourceRange);
for (auto it = Range.first; it != Range.second; ++it)
{
auto PPEntity = (*it);
auto Entity = WalkPreprocessedEntity(Decl, PPEntity);
if (!Entity) continue;
if (Entity->Location == MacroLocation::Unknown)
Entity->Location = macroLocation;
} }
} }

2
src/CppParser/Parser.h

@ -79,6 +79,8 @@ protected:
void WalkVTable(clang::CXXRecordDecl* RD, Class* C); void WalkVTable(clang::CXXRecordDecl* RD, Class* C);
VTableLayout WalkVTableLayout(const clang::VTableLayout& VTLayout); VTableLayout WalkVTableLayout(const clang::VTableLayout& VTLayout);
VTableComponent WalkVTableComponent(const clang::VTableComponent& Component); VTableComponent WalkVTableComponent(const clang::VTableComponent& Component);
PreprocessedEntity* WalkPreprocessedEntity(Declaration* Decl,
clang::PreprocessedEntity* PPEntity);
// Clang helpers // Clang helpers
SourceLocationKind GetLocationKind(const clang::SourceLocation& Loc); SourceLocationKind GetLocationKind(const clang::SourceLocation& Loc);

68
src/Parser/Parser.cpp

@ -2033,31 +2033,21 @@ bool Parser::GetDeclText(clang::SourceRange SR, std::string& Text)
return !Invalid && !Text.empty(); return !Invalid && !Text.empty();
} }
void Parser::HandlePreprocessedEntities(CppSharp::AST::Declaration^ Decl, CppSharp::AST::PreprocessedEntity^ Parser::WalkPreprocessedEntity(
clang::SourceRange sourceRange, CppSharp::AST::Declaration^ Decl, clang::PreprocessedEntity* PPEntity)
CppSharp::AST::MacroLocation macroLocation)
{ {
if (sourceRange.isInvalid()) return;
auto& SourceMgr = C->getSourceManager();
auto isBefore = SourceMgr.isBeforeInTranslationUnit(sourceRange.getEnd(),
sourceRange.getBegin());
if (isBefore) return;
assert(!SourceMgr.isBeforeInTranslationUnit(sourceRange.getEnd(),
sourceRange.getBegin()));
using namespace clang; using namespace clang;
auto PPRecord = C->getPreprocessor().getPreprocessingRecord();
auto Range = PPRecord->getPreprocessedEntitiesInRange(sourceRange);
for (auto it = Range.first; it != Range.second; ++it) for (unsigned I = 0, E = Decl->PreprocessedEntities->Count;
I != E; ++I)
{ {
PreprocessedEntity* PPEntity = (*it); auto Entity = Decl->PreprocessedEntities[I];
if (Entity->OriginalPtr == System::IntPtr(PPEntity))
return Entity;
}
CppSharp::AST::PreprocessedEntity^ Entity; CppSharp::AST::PreprocessedEntity^ Entity;
switch(PPEntity->getKind()) switch(PPEntity->getKind())
{ {
case PreprocessedEntity::MacroExpansionKind: case PreprocessedEntity::MacroExpansionKind:
@ -2067,7 +2057,7 @@ void Parser::HandlePreprocessedEntities(CppSharp::AST::Declaration^ Decl,
std::string Text; std::string Text;
if (!GetDeclText(PPEntity->getSourceRange(), Text)) if (!GetDeclText(PPEntity->getSourceRange(), Text))
continue; return nullptr;
static_cast<CppSharp::AST::MacroExpansion^>(Entity)->Text = static_cast<CppSharp::AST::MacroExpansion^>(Entity)->Text =
clix::marshalString<clix::E_UTF8>(Text); clix::marshalString<clix::E_UTF8>(Text);
@ -2080,12 +2070,44 @@ void Parser::HandlePreprocessedEntities(CppSharp::AST::Declaration^ Decl,
break; break;
} }
default: default:
continue; return nullptr;
} }
Entity->Location = macroLocation; Entity->OriginalPtr = System::IntPtr(PPEntity);
Decl->PreprocessedEntities->Add(Entity); Decl->PreprocessedEntities->Add(Entity);
return Entity;
}
void Parser::HandlePreprocessedEntities(CppSharp::AST::Declaration^ Decl,
clang::SourceRange sourceRange,
CppSharp::AST::MacroLocation macroLocation)
{
if (sourceRange.isInvalid()) return;
auto& SourceMgr = C->getSourceManager();
auto isBefore = SourceMgr.isBeforeInTranslationUnit(sourceRange.getEnd(),
sourceRange.getBegin());
if (isBefore) return;
assert(!SourceMgr.isBeforeInTranslationUnit(sourceRange.getEnd(),
sourceRange.getBegin()));
using namespace clang;
auto PPRecord = C->getPreprocessor().getPreprocessingRecord();
auto Range = PPRecord->getPreprocessedEntitiesInRange(sourceRange);
for (auto it = Range.first; it != Range.second; ++it)
{
PreprocessedEntity* PPEntity = (*it);
auto Entity = WalkPreprocessedEntity(Decl, PPEntity);
if (!Entity) continue;
if (Entity->Location == CppSharp::AST::MacroLocation::Unknown)
Entity->Location = macroLocation;
} }
} }

2
src/Parser/Parser.h

@ -85,6 +85,8 @@ protected:
void WalkVTable(clang::CXXRecordDecl* RD, CppSharp::AST::Class^ C); void WalkVTable(clang::CXXRecordDecl* RD, CppSharp::AST::Class^ C);
CppSharp::AST::VTableLayout^ WalkVTableLayout(const clang::VTableLayout& VTLayout); CppSharp::AST::VTableLayout^ WalkVTableLayout(const clang::VTableLayout& VTLayout);
CppSharp::AST::VTableComponent WalkVTableComponent(const clang::VTableComponent& Component); CppSharp::AST::VTableComponent WalkVTableComponent(const clang::VTableComponent& Component);
CppSharp::AST::PreprocessedEntity^ WalkPreprocessedEntity(CppSharp::AST::Declaration^ Decl,
clang::PreprocessedEntity* PPEntity);
// Clang helpers // Clang helpers
SourceLocationKind GetLocationKind(const clang::SourceLocation& Loc); SourceLocationKind GetLocationKind(const clang::SourceLocation& Loc);

Loading…
Cancel
Save