diff --git a/clang/test/AST/ast-print-attr.c b/clang/test/AST/ast-print-attr.c --- a/clang/test/AST/ast-print-attr.c +++ b/clang/test/AST/ast-print-attr.c @@ -28,0 +29,3 @@ + +// CHECK: int fun_annotate() __attribute__((annotate("annotation"))) +int fun_annotate() __attribute__((annotate("annotation"))); diff --git a/clang/utils/TableGen/ClangAttrEmitter.cpp b/clang/utils/TableGen/ClangAttrEmitter.cpp --- a/clang/utils/TableGen/ClangAttrEmitter.cpp +++ b/clang/utils/TableGen/ClangAttrEmitter.cpp @@ -776,4 +776,2 @@ - OS << " bool isFirst = true;\n" - << " for (const auto &Val : " << RangeName << "()) {\n" - << " if (isFirst) isFirst = false;\n" - << " else OS << \", \";\n"; + OS << " for (const auto &Val : " << RangeName << "()) {\n" + << " Comma(OS, IsFirstArgument);\n"; @@ -1431,4 +1429,6 @@ - OS << " switch (getAttributeSpellingListIndex()) {\n" - " default:\n" - " llvm_unreachable(\"Unknown attribute spelling!\");\n" - " break;\n"; + OS << " bool IsFirstArgument = true; (void)IsFirstArgument;\n" + << " unsigned TrailingOmittedArgs = 0; (void)TrailingOmittedArgs;\n" + << " switch (getAttributeSpellingListIndex()) {\n" + << " default:\n" + << " llvm_unreachable(\"Unknown attribute spelling!\");\n" + << " break;\n"; @@ -1479,3 +1479,2 @@ - OS << - " case " << I << " : {\n" - " OS << \"" << Prefix << Spelling; + OS << " case " << I << " : {\n" + << " OS << \"" << Prefix << Spelling << "\";\n"; @@ -1484 +1482,0 @@ - OS << "\";\n"; @@ -1493 +1491 @@ - OS << "("; + OS << " OS << \"("; @@ -1495 +1493 @@ - OS << ")"; + OS << ")\";\n"; @@ -1497 +1495 @@ - OS << "("; + OS << " OS << \"("; @@ -1499 +1497 @@ - OS << ")"; + OS << ")\";\n"; @@ -1505 +1502,0 @@ - unsigned TrailingOptArgs = 0; @@ -1519,3 +1515,0 @@ - if (!TrailingOptArgs++) - OS << "\";\n" - << " unsigned TrailingOmittedArgs = 0;\n"; @@ -1525,9 +1518,0 @@ - if (TrailingOptArgs) - OS << " OS << \""; - if (TrailingOptArgs < NonFakeArgs) - OS << "("; - else if (TrailingOptArgs) - OS << "\";\n" - << " if (TrailingOmittedArgs < " << NonFakeArgs << ")\n" - << " OS << \"(\";\n" - << " OS << \""; @@ -1538,10 +1522,0 @@ - if (ArgIndex) { - if (ArgIndex >= NonFakeArgs - TrailingOptArgs) - OS << "\";\n" - << " if (" << ArgIndex << " < " << NonFakeArgs - << " - TrailingOmittedArgs)\n" - << " OS << \", \";\n" - << " OS << \""; - else - OS << ", "; - } @@ -1550,3 +1525,5 @@ - OS << "\";\n" - << " if (!(" << IsOmitted << ")) {\n" - << " OS << \""; + OS << " if (!(" << IsOmitted << ")) {\n"; + // Variadic arguments print their own leading comma. + if (!arg->isVariadic()) + OS << " Comma(OS, IsFirstArgument);\n"; + OS << " OS << \""; @@ -1553,0 +1531 @@ + OS << "\";\n"; @@ -1555,3 +1533 @@ - OS << "\";\n" - << " }\n" - << " OS << \""; + OS << " }\n"; @@ -1560,7 +1536,3 @@ - if (TrailingOptArgs < NonFakeArgs) - OS << ")"; - else if (TrailingOptArgs) - OS << "\";\n" - << " if (TrailingOmittedArgs < " << NonFakeArgs << ")\n" - << " OS << \")\";\n" - << " OS << \""; + if (ArgIndex != 0) + OS << " if (!IsFirstArgument)\n" + << " OS << \")\";\n"; @@ -1568,6 +1540,3 @@ - - OS << Suffix + "\";\n"; - - OS << - " break;\n" - " }\n"; + OS << " OS << \"" << Suffix << "\";\n" + << " break;\n" + << " }\n"; @@ -2281,0 +2251,11 @@ + // Helper to print the starting character of an attribute argument. If there + // hasn't been an argument yet, it prints an opening parenthese; otherwise it + // prints a comma. + OS << "static inline void Comma(raw_ostream& OS, bool& IsFirst) {\n" + << " if (IsFirst) {\n" + << " IsFirst = false;\n" + << " OS << \"(\";\n" + << " } else\n" + << " OS << \", \";\n" + << "}\n"; +