[PATCH] Improve attributes printing.

Richard Smith richard at metafoo.co.uk
Tue Mar 19 14:24:35 PDT 2013



================
Comment at: lib/AST/DeclPrinter.cpp:338
@@ -337,3 +337,3 @@
   D->getUnderlyingType().print(Out, Policy, D->getName());
-  prettyPrintAttributes(D);
+  prettyPrintAttributes(D, Attr::LK_AfterDeclaratorID);
 }
----------------
Isn't this LK_EndOfDecl?

================
Comment at: lib/AST/DeclPrinter.cpp:365
@@ -364,3 +364,3 @@
   }
-  prettyPrintAttributes(D);
+  prettyPrintAttributes(D, Attr::LK_AfterDeclaratorID);
 }
----------------
This one looks really odd. We should be printing attributes after the enum/enum class/enum struct keywords (the closest match is LK_AfterDeclaratorID for those, even though they're before the declarator-id). I don't think there are *any* attributes which appertain to an enum, and go here, after the enum body.

================
Comment at: lib/AST/DeclPrinter.cpp:391
@@ -390,2 +390,3 @@
 void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
+  prettyPrintAttributes(D, Attr::LK_StartOfDecl);
   CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(D);
----------------
Can we generalize this to print LK_StartOfDecl attributes at the start of *every* declaration?

================
Comment at: lib/AST/DeclPrinter.cpp:633
@@ -631,3 +632,3 @@
   }
-  prettyPrintAttributes(D);
+  prettyPrintAttributes(D, Attr::LK_AfterDeclaratorID);
 }
----------------
This is at EndOfDecl, not AfterDeclaratorID. And even then, it should go before the initializer. AfterDeclaratorID happens inside the type printer (for instance, try pretty-printing something like: "int a [[gnu::used]] [3];").

================
Comment at: lib/AST/DeclPrinter.cpp:677
@@ -675,3 +676,3 @@
   }
-  prettyPrintAttributes(D);
+  prettyPrintAttributes(D, Attr::LK_AfterDeclaratorID);
 }
----------------
Likewise.

================
Comment at: lib/AST/DeclPrinter.cpp:729
@@ -727,3 +728,3 @@
 void DeclPrinter::VisitEmptyDecl(EmptyDecl *D) {
-  prettyPrintAttributes(D);
+  prettyPrintAttributes(D, Attr::LK_AfterDeclaratorID);
 }
----------------
We should print all forms of attributes here.

================
Comment at: utils/TableGen/ClangAttrEmitter.cpp:811
@@ +810,3 @@
+    if (Variety == "CXX11")
+      OS << "    if (LK == 0 || LK == 1 || LK == 3) {\n";
+
----------------
Each attribute should only be printed at one location.

================
Comment at: include/clang/AST/Attr.h:67-76
@@ +66,12 @@
+  /// could appear. It is used to print out the attribute at right location.
+  enum LocationKind {
+    /// \brief Attribute appears at the start of a declaration.
+    LK_StartOfDecl,
+    /// \brief Attribute appears after the declarator-id.
+    LK_AfterDeclaratorID,
+    /// \brief Attribute appears at the end of a declaration.
+    LK_EndOfDecl,
+    /// \brief Attribute appears at the start of a statement.
+    LK_StartOfStmt
+  };
+
----------------
Maybe make this a bitmask? For instance, for an EmptyDecl, we want to print all forms.

The general principle here should be that each attribute will be asked to be printed once for each location, and will print itself in the appropriate place.


http://llvm-reviews.chandlerc.com/D395



More information about the cfe-commits mailing list