[cfe-dev] extern "C" support

Chris Lattner clattner at apple.com
Sat Dec 22 17:28:44 PST 2007

On Dec 21, 2007, at 2:07 PM, Mike Stump wrote:

> This adds initial extern "C" support...  It includes ast, sema, ast
> printer, and stub codegen but does not include ast dumper.

Hi Mike,

Very cool, thanks for tackling this!

Please attach patches as attachments, not inline.  If you're using  
'mail', just name the file "whatever.patch" and this will happen.   
Until this happens, I can't properly review the patch as it's all  
wrapped and nastified :(

Some random thoughts:

+++ ./Driver/ASTConsumers.cpp	2007-12-21 13:58:52.000000000 -0800
+void DeclPrinter:: PrintDecl(Decl *D) {
+  if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
+    PrintFunctionDeclStart(FD);
+    if (FD->getBody()) {
+      Out << ' ';
+      FD->getBody()->printPretty(Out);
+      Out << '\n';
+    }
+  } else if (isa<ObjcMethodDecl>(D)) {
+    // Do nothing, methods definitions are printed in
+    // PrintObjcImplementationDecl.
+  } else if (TypedefDecl *TD = dyn_cast<TypedefD

This refactoring looks independently useful, please submit it  

+/// LinkageSpecDecl - This represents a linkage specification.
+class LinkageSpecDecl : public Decl {

Please give an example of what a linkage spec is in the header.  At  
some point, we probably want to make a DeclCXX.h file, but deferring  
this for now is fine.

+  /// Language - The language for this linkage specification.
+  Language language;
+  bool inside_braces;
+  Decl *D;

inside_braces is good for "remembering" the original source, but  
unless there is a client, I'd suggest not including it.  How does your  
Decl structure handle stuff like:

extern "C" {
   int x;  int y;

Is there just one LinkageSpecDecl?

+++ ./Parse/Parser.cpp	2007-12-21 13:58:52.000000000 -0800
@@ -378,6 +378,12 @@ Parser::DeclTy *Parser::ParseDeclaration
        return ParseObjCAtInterfaceDeclaration(AtLoc,

+  if (Tok.is(tok::string_literal)
+      && DS.getStorageClassSpec() == DeclSpec::SCS_extern)
+    {
+      return ParseLinkage(Declarator::FileContext);

I'm not sure if this is sufficient: does this allow 'extern int "C"'   
or 'extern inline "C"'  The DeclSpec::getParsedSpecifiers() method can  
be used to make sure there is just a storage class specified.

+Sema::DeclTy* Sema::ActOnLinkageSpec(SourceLocation Loc,
+				     std::string Lang,

Please don't pass std::strings by-value, pass by const reference, or  
better yet, pass in the string as a const char* or something like that.


More information about the cfe-dev mailing list