extern "C" parsing

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

extern "C" parsing

Jay L. T. Cornwall-2
Hi,

I am porting one of our internal tools built upon the ROSE
source-to-source compiler to LLVM/Clang, to improve its scalability.

With the program:

extern "C" {
typedef struct {} MyStruct;
}

MyStruct a;

The clang driver from SVN successfully parses this. My library-based
interface to clang, however, gives this error:

test.cpp:5:1: error: unknown type name 'MyStruct'

The presence of an extern "C" {} block appears to hide the type. It
parses correctly without the block. I have set LangOptions::CPlusPlus
but have been unable to locate an option which controls this behaviour.

Could you point me in the right direction?

--
Jay L. T. Cornwall, Software Engineer
The Foundry, 1 Wardour Street, London. W1D 6PA
Tel: +44 (0)20 7434 0449, Fax: +44 (0)20 7434 1550, Web:
www.thefoundry.co.uk

The Foundry Visionmongers Ltd • Registered in England and Wales No: 4642027
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: extern "C" parsing

Douglas Gregor

On Feb 16, 2010, at 8:11 AM, Jay L. T. Cornwall wrote:

> Hi,
>
> I am porting one of our internal tools built upon the ROSE
> source-to-source compiler to LLVM/Clang, to improve its scalability.
>
> With the program:
>
> extern "C" {
> typedef struct {} MyStruct;
> }
>
> MyStruct a;
>
> The clang driver from SVN successfully parses this. My library-based
> interface to clang, however, gives this error:
>
> test.cpp:5:1: error: unknown type name 'MyStruct'
>
> The presence of an extern "C" {} block appears to hide the type. It
> parses correctly without the block. I have set LangOptions::CPlusPlus
> but have been unable to locate an option which controls this behaviour.

That's very strange. CPlusPlus is the only option that should matter here, because C++ name lookup involves looking into linkage specifications.

> Could you point me in the right direction?


I'm not sure where to point you. The lookup code in question is Sema::LookupName (in lib/Sema/SemaLookup.cpp). I guess you could make sure that it's performing C++ name lookup when it looks for 'MyStruct'.

Perhaps we should step back a bit, however: how are you setting up your library interface to Clang? Are you using CompilerInstance/CompilerInvocation?

        - Doug
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: extern "C" parsing

Jay L. T. Cornwall-2
Hi Doug,

>> The clang driver from SVN successfully parses this. My
>> library-based interface to clang, however, gives this error:
>>
>> test.cpp:5:1: error: unknown type name 'MyStruct'
>>
>> The presence of an extern "C" {} block appears to hide the type. It
>>  parses correctly without the block. I have set
>> LangOptions::CPlusPlus but have been unable to locate an option
>> which controls this behaviour.

> That's very strange. CPlusPlus is the only option that should matter
> here, because C++ name lookup involves looking into linkage
> specifications.

I tracked this down in the end. With the -fsyntax-only parameter Clang
parses the program correctly. With -parse-noop it gives the
aforementioned error.

I believe this is because -parse-noop uses MinimalAction whereas
-fsyntax-only invokes SyntaxOnlyAction. I traced the program error in
the former case to MinimalAction::getTypeName failing to acknowledge
MyStruct as a type name.

> Perhaps we should step back a bit, however: how are you setting up
> your library interface to Clang? Are you using
> CompilerInstance/CompilerInvocation

Initially I had followed this tutorial:

http://amnoid.de/tmp/clangtut/tut.html

This information appears to be quite outdated now. I subsequently
remodelled our application to Clang to use the
CompilerInstace/CompilerInvocation interface and this now works.

With Clang successfully parsing a complex C++ file (with many deep Boost
MPL-based templates and other magic) I now have access to the ASTContext
to begin porting our semantic analyses. The (speed/memory) performance
issues we had with ROSE have gone away with Clang. You guys have done
some nice work!

--
Jay L. T. Cornwall, Software Engineer
The Foundry, 1 Wardour Street, London. W1D 6PA
Tel: +44 (0)20 7434 0449, Fax: +44 (0)20 7434 1550, Web:
www.thefoundry.co.uk

The Foundry Visionmongers Ltd • Registered in England and Wales No: 4642027
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: extern "C" parsing

Douglas Gregor

On Feb 17, 2010, at 11:25 PM, Jay L. T. Cornwall wrote:

> Hi Doug,
>
>>> The clang driver from SVN successfully parses this. My
>>> library-based interface to clang, however, gives this error:
>>> test.cpp:5:1: error: unknown type name 'MyStruct'
>>> The presence of an extern "C" {} block appears to hide the type. It
>>> parses correctly without the block. I have set
>>> LangOptions::CPlusPlus but have been unable to locate an option
>>> which controls this behaviour.
>
>> That's very strange. CPlusPlus is the only option that should matter
>> here, because C++ name lookup involves looking into linkage
>> specifications.
>
> I tracked this down in the end. With the -fsyntax-only parameter Clang parses the program correctly. With -parse-noop it gives the aforementioned error.
>
> I believe this is because -parse-noop uses MinimalAction whereas -fsyntax-only invokes SyntaxOnlyAction. I traced the program error in the former case to MinimalAction::getTypeName failing to acknowledge MyStruct as a type name.

-parse-noop will never work for C++.

>> Perhaps we should step back a bit, however: how are you setting up
>> your library interface to Clang? Are you using
>> CompilerInstance/CompilerInvocation
>
> Initially I had followed this tutorial:
>
> http://amnoid.de/tmp/clangtut/tut.html
>
> This information appears to be quite outdated now. I subsequently remodelled our application to Clang to use the CompilerInstace/CompilerInvocation interface and this now works.

Yes, that's out of date. We're hoping to find time to write a good tutorial for CompilerInstance/CompilerInvocation.

> With Clang successfully parsing a complex C++ file (with many deep Boost MPL-based templates and other magic) I now have access to the ASTContext to begin porting our semantic analyses. The (speed/memory) performance issues we had with ROSE have gone away with Clang. You guys have done some nice work!

Thanks, glad to hear it!

        - Doug
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev