get predefined typedefs and vars

Hi,

I'm using a decl_iterator to loop over Decl::Vars and Decl::Typedefs and printing them to a file.

Is there a way to find out if this Var or Typedef was already predefined or actually coming from my parsed source file?

So I do not want to dump stuff such as

extern char* __tzname[2] or

typedef unsigned long __u_quad_t

Thanks for your help,
Max

Hi,

I'm using a decl_iterator to loop over Decl::Vars and Decl::Typedefs and
printing them to a file.

Is there a way to find out if this Var or Typedef was already predefined
or actually coming from my parsed source file?

The best way to do this is to use the SourceManager::isFromMainFile method, which tells you if a source location is in the main file. Note that you'll probably want to pass in an instantiation location to handle things with macros.

-Chris

Hi,

thanks for the quick reply.

Is there a way to find out if this Var or Typedef was already predefined
or actually coming from my parsed source file?

The best way to do this is to use the SourceManager::isFromMainFile method

I tried using

...
case Decl::Typedef:

if (s.getSourceManager().isFromMainFile(it->getSourceRange().getBegin())
  it->dump();
...

But then I get an assertion:

clang-cc: clang/lib/Basic/SourceManager.cpp:429: clang::FileID clang::SourceManager::getFileIDSlow(unsigned int) const: Assertion `SLocOffset && "Invalid FileID"' failed.

The first typedef to hit this line is __int128_t...

Am I doing sth. wrong here?

Thanks,
Max

Hi,

thanks for the quick reply.

Is there a way to find out if this Var or Typedef was already predefined
or actually coming from my parsed source file?

The best way to do this is to use the SourceManager::isFromMainFile method

I tried using

...
case Decl::Typedef:

if (s.getSourceManager().isFromMainFile(it->getSourceRange().getBegin())
  it->dump();
...

But then I get an assertion:

clang-cc: clang/lib/Basic/SourceManager.cpp:429: clang::FileID
clang::SourceManager::getFileIDSlow(unsigned int) const: Assertion
`SLocOffset && "Invalid FileID"' failed.

The first typedef to hit this line is __int128_t...

I'm not sure what "it" is here, but try Decl::getLocation() instead. You should check to make sure it isn't an invalid source location though with SourceLocation::isValid()

-Chris

Hi,

>I'm not sure what "it" is here, but try Decl::getLocation() instead. >You should check to make sure it isn't an invalid source location >though with SourceLocation::isValid()

works perfectly, thanks!

Best,
Max