llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[clang] clang -ast-dump and -ast-print doesn't keep the content of annotate_type

Open neko-para opened this issue 3 years ago • 3 comments

1.cpp:

void abc(int [[clang::annotate_type("foobar")]] x);

clang -Xclang -ast-dump -fsyntax-only 1.cpp output:

`-FunctionDecl 0x22d8ed7ab58 <1.cpp:1:1, col:50> col:6 abc 'void (int [[clang::annotate_type(...)]])'
  `-ParmVarDecl 0x22d8ed7aa60 <col:10, col:49> col:49 x 'int [[clang::annotate_type(...)]]':'int'

clang -Xclang -ast-print -fsyntax-only 1.cpp output:

void abc(int x [[clang::annotate_type(...)]]);

As the above shown, "foobar" become .... I'm not sure if this is by design, not implemented yet or just a bug? Is there any commandline option to enable it just like comments (-fparse-all-comments)?

neko-para avatar Nov 19 '23 13:11 neko-para

@llvm/issue-subscribers-clang-frontend

Author: nekosu (neko-para)

`1.cpp`:
void abc(int [[clang::annotate_type("foobar")]] x);

clang -Xclang -ast-dump -fsyntax-only 1.cpp output:

`-FunctionDecl 0x22d8ed7ab58 &lt;1.cpp:1:1, col:50&gt; col:6 abc 'void (int [[clang::annotate_type(...)]])'
  `-ParmVarDecl 0x22d8ed7aa60 &lt;col:10, col:49&gt; col:49 x 'int [[clang::annotate_type(...)]]':'int'

clang -Xclang -ast-print -fsyntax-only 1.cpp output:

void abc(int x [[clang::annotate_type(...)]]);

As the above shown, "foobar" become .... I'm not sure if this is by design, not implemented yet or just a bug? Is there any commandline option to enable it just like comments (-fparse-all-comments)?

llvmbot avatar Nov 19 '23 15:11 llvmbot

Maybe related: https://github.com/llvm/llvm-project/issues/62996

shafik avatar Nov 28 '23 22:11 shafik

I'm not sure if this is by design, not implemented yet or just a bug?

It's not implemented. The printing is done here:

  if (T->getAttrKind() == attr::AnnotateType) {
    // FIXME: Print the attribute arguments once we have a way to retrieve these
    // here. For the meantime, we just print `[[clang::annotate_type(...)]]`
    // without the arguments so that we know at least that we had _some_
    // annotation on the type.
    OS << " [[clang::annotate_type(...)]]";
    return;
  }

Using instead the printPretty method of the attribute

-    OS << " [[clang::annotate_type(...)]]";
+    OS << " ";
+    T->getAttr()->printPretty(OS, Policy);

works when it has 1 argument but prints a pointer address with 2+ arguments:

void abc(int x [[clang::annotate_type("foobar", 2)]]); // Input
void abc(int x [[clang::annotate_type("foobar", 0x55b69825a3c0)]]); // Output

So this seems to be another bug which comes from the auto-generated AnnotateTypeAttr::printPretty method in AttrImpl.inc:

     OS << "[[clang::annotate_type";                                                                                                                                                         
     DelimitAttributeArgument(OS, IsFirstArgument);                                                                                                                                          
     OS << "\"" << getAnnotation() << "\"";                                                                                                                                                  
     OS << "";                                                                                                                                                                               
   for (const auto &Val : args()) {                                                                                                                                                          
     DelimitAttributeArgument(OS, IsFirstArgument);                                                                                                                                          
     OS << Val;                                                                                                                                                                              
   }

args() iterates over Expr *, so the pointer is printed. The same happens for declaration attributes, e.g. int x __attribute__((annotate("a", 1))); under -ast-print, so it is independent of TypePrinter.


What I still don't understand yet is why the TypePrinter does not use the printPretty methods for the attributes already implemented in AttrImpl.inc. These functions are generated from Attr.td by clang/utils/TableGen/ClangAttrEmitter.cpp. Note also that the function printing the ... has a FIXME

void TypePrinter::printAttributedAfter(const AttributedType *T, raw_ostream &OS) {                                                                                                                                   
   // FIXME: Generate this with TableGen.

to generate it with TableGen. Would using getAttr()->printPretty() where available be the intended direction, or is something else planned?

max-winkler avatar Nov 07 '26 06:11 max-winkler