From 3796d1539a39b999fd50bed7aea726ef6f845e17 Mon Sep 17 00:00:00 2001 From: Douglas Gregor Date: Thu, 2 May 2013 23:08:12 +0000 Subject: Use attribute argument information to determine when to parse attribute arguments as expressions. This change partly addresses a heinous problem we have with the parsing of attribute arguments that are a lone identifier. Previously, we would end up parsing the 'align' attribute of this as an expression "(Align)": template class my_aligned_storage { __attribute__((align((Align)))) char storage[Size]; }; while this would parse as a "parameter name" 'Align': template class my_aligned_storage { __attribute__((align(Align))) char storage[Size]; }; The code that handles the alignment attribute would completely ignore the parameter name, so the while the first of these would do what's expected, the second would silently be equivalent to template class my_aligned_storage { __attribute__((align)) char storage[Size]; }; i.e., use the maximal alignment rather than the specified alignment. Address this by sniffing the "Args" provided in the TableGen description of attributes. If the first argument is "obviously" something that should be treated as an expression (rather than an identifier to be matched later), parse it as an expression. Fixes . git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@180970 91177308-0d34-0410-b5e6-96231b3b80d8 --- utils/TableGen/ClangAttrEmitter.cpp | 43 +++++++++++++++++++++++++++++++++++++ utils/TableGen/TableGen.cpp | 8 +++++++ utils/TableGen/TableGenBackends.h | 1 + 3 files changed, 52 insertions(+) (limited to 'utils') diff --git a/utils/TableGen/ClangAttrEmitter.cpp b/utils/TableGen/ClangAttrEmitter.cpp index 7c8603fc6c..36de06e2df 100644 --- a/utils/TableGen/ClangAttrEmitter.cpp +++ b/utils/TableGen/ClangAttrEmitter.cpp @@ -971,6 +971,49 @@ void EmitClangAttrClass(RecordKeeper &Records, raw_ostream &OS) { OS << "#endif\n"; } +// Emits the LateParsed property for attributes. +void EmitClangAttrExprArgsList(RecordKeeper &Records, raw_ostream &OS) { + emitSourceFileHeader("llvm::StringSwitch code to match attributes with " + "expression arguments", OS); + + std::vector Attrs = Records.getAllDerivedDefinitions("Attr"); + + for (std::vector::iterator I = Attrs.begin(), E = Attrs.end(); + I != E; ++I) { + Record &Attr = **I; + + // Determine whether the first argument is something that is always + // an expression. + std::vector Args = Attr.getValueAsListOfDefs("Args"); + if (Args.empty() || Args[0]->getSuperClasses().empty()) + continue; + + // Check whether this is one of the argument kinds that implies an + // expression. + // FIXME: Aligned is weird. + if (!llvm::StringSwitch(Args[0]->getSuperClasses().back()->getName()) + .Case("AlignedArgument", true) + .Case("BoolArgument", true) + .Case("DefaultIntArgument", true) + .Case("IntArgument", true) + .Case("StringArgument", true) + .Case("ExprArgument", true) + .Case("UnsignedArgument", true) + .Case("VariadicUnsignedArgument", true) + .Case("VariadicExprArgument", true) + .Default(false)) + continue; + + std::vector Spellings = Attr.getValueAsListOfDefs("Spellings"); + + for (std::vector::const_iterator I = Spellings.begin(), + E = Spellings.end(); I != E; ++I) { + OS << ".Case(\"" << (*I)->getValueAsString("Name") << "\", " + << "true" << ")\n"; + } + } +} + // Emits the class method definitions for attributes. void EmitClangAttrImpl(RecordKeeper &Records, raw_ostream &OS) { emitSourceFileHeader("Attribute classes' member function definitions", OS); diff --git a/utils/TableGen/TableGen.cpp b/utils/TableGen/TableGen.cpp index 3df8940b05..12e1c47725 100644 --- a/utils/TableGen/TableGen.cpp +++ b/utils/TableGen/TableGen.cpp @@ -24,6 +24,7 @@ using namespace clang; enum ActionType { GenClangAttrClasses, + GenClangAttrExprArgsList, GenClangAttrImpl, GenClangAttrList, GenClangAttrPCHRead, @@ -62,6 +63,10 @@ namespace { "Generate option parser implementation"), clEnumValN(GenClangAttrClasses, "gen-clang-attr-classes", "Generate clang attribute clases"), + clEnumValN(GenClangAttrExprArgsList, + "gen-clang-attr-expr-args-list", + "Generate a clang attribute expression " + "arguments list"), clEnumValN(GenClangAttrImpl, "gen-clang-attr-impl", "Generate clang attribute implementations"), clEnumValN(GenClangAttrList, "gen-clang-attr-list", @@ -143,6 +148,9 @@ bool ClangTableGenMain(raw_ostream &OS, RecordKeeper &Records) { case GenClangAttrClasses: EmitClangAttrClass(Records, OS); break; + case GenClangAttrExprArgsList: + EmitClangAttrExprArgsList(Records, OS); + break; case GenClangAttrImpl: EmitClangAttrImpl(Records, OS); break; diff --git a/utils/TableGen/TableGenBackends.h b/utils/TableGen/TableGenBackends.h index 03708b6a76..0ff33d775c 100644 --- a/utils/TableGen/TableGenBackends.h +++ b/utils/TableGen/TableGenBackends.h @@ -30,6 +30,7 @@ void EmitClangASTNodes(RecordKeeper &RK, raw_ostream &OS, const std::string &N, const std::string &S); void EmitClangAttrClass(RecordKeeper &Records, raw_ostream &OS); +void EmitClangAttrExprArgsList(RecordKeeper &Records, raw_ostream &OS); void EmitClangAttrImpl(RecordKeeper &Records, raw_ostream &OS); void EmitClangAttrList(RecordKeeper &Records, raw_ostream &OS); void EmitClangAttrPCHRead(RecordKeeper &Records, raw_ostream &OS); -- cgit v1.2.3-18-g5258