diff options
Diffstat (limited to 'utils/TableGen')
| -rw-r--r-- | utils/TableGen/ClangAttrEmitter.cpp | 297 | ||||
| -rw-r--r-- | utils/TableGen/ClangCommentHTMLNamedCharacterReferenceEmitter.cpp | 2 | ||||
| -rw-r--r-- | utils/TableGen/ClangDiagnosticsEmitter.cpp | 1083 | ||||
| -rw-r--r-- | utils/TableGen/ClangOptionDocEmitter.cpp | 28 | ||||
| -rw-r--r-- | utils/TableGen/ClangSACheckersEmitter.cpp | 2 | ||||
| -rw-r--r-- | utils/TableGen/NeonEmitter.cpp | 180 | ||||
| -rw-r--r-- | utils/TableGen/TableGen.cpp | 5 | ||||
| -rw-r--r-- | utils/TableGen/TableGenBackends.h | 1 |
8 files changed, 1187 insertions, 411 deletions
diff --git a/utils/TableGen/ClangAttrEmitter.cpp b/utils/TableGen/ClangAttrEmitter.cpp index b0e2ddd91362..0bf7a07cf6e1 100644 --- a/utils/TableGen/ClangAttrEmitter.cpp +++ b/utils/TableGen/ClangAttrEmitter.cpp @@ -87,6 +87,8 @@ GetFlattenedSpellings(const Record &Attr) { } else if (Variety == "Clang") { Ret.emplace_back("GNU", Name, "", false); Ret.emplace_back("CXX11", Name, "clang", false); + if (Spelling->getValueAsBit("AllowInC")) + Ret.emplace_back("C2x", Name, "clang", false); } else Ret.push_back(FlattenedSpelling(*Spelling)); } @@ -102,6 +104,7 @@ static std::string ReadPCHRecord(StringRef type) { .Case("Expr *", "Record.readExpr()") .Case("IdentifierInfo *", "Record.getIdentifierInfo()") .Case("StringRef", "Record.readString()") + .Case("ParamIdx", "ParamIdx::deserialize(Record.readInt())") .Default("Record.readInt()"); } @@ -120,6 +123,7 @@ static std::string WritePCHRecord(StringRef type, StringRef name) { .Case("Expr *", "AddStmt(" + std::string(name) + ");\n") .Case("IdentifierInfo *", "AddIdentifierRef(" + std::string(name) + ");\n") .Case("StringRef", "AddString(" + std::string(name) + ");\n") + .Case("ParamIdx", "push_back(" + std::string(name) + ".serialize());\n") .Default("push_back(" + std::string(name) + ");\n"); } @@ -229,6 +233,7 @@ namespace { virtual void writePCHReadArgs(raw_ostream &OS) const = 0; virtual void writePCHReadDecls(raw_ostream &OS) const = 0; virtual void writePCHWrite(raw_ostream &OS) const = 0; + virtual std::string getIsOmitted() const { return "false"; } virtual void writeValue(raw_ostream &OS) const = 0; virtual void writeDump(raw_ostream &OS) const = 0; virtual void writeDumpChildren(raw_ostream &OS) const {} @@ -296,23 +301,29 @@ namespace { std::string(getUpperName()) + "()"); } + std::string getIsOmitted() const override { + if (type == "IdentifierInfo *") + return "!get" + getUpperName().str() + "()"; + if (type == "ParamIdx") + return "!get" + getUpperName().str() + "().isValid()"; + return "false"; + } + void writeValue(raw_ostream &OS) const override { - if (type == "FunctionDecl *") { + if (type == "FunctionDecl *") OS << "\" << get" << getUpperName() << "()->getNameInfo().getAsString() << \""; - } else if (type == "IdentifierInfo *") { - OS << "\";\n"; - if (isOptional()) - OS << " if (get" << getUpperName() << "()) "; - else - OS << " "; - OS << "OS << get" << getUpperName() << "()->getName();\n"; - OS << " OS << \""; - } else if (type == "TypeSourceInfo *") { + else if (type == "IdentifierInfo *") + // Some non-optional (comma required) identifier arguments can be the + // empty string but are then recorded as a nullptr. + OS << "\" << (get" << getUpperName() << "() ? get" << getUpperName() + << "()->getName() : \"\") << \""; + else if (type == "TypeSourceInfo *") OS << "\" << get" << getUpperName() << "().getAsString() << \""; - } else { + else if (type == "ParamIdx") + OS << "\" << get" << getUpperName() << "().getSourceIndex() << \""; + else OS << "\" << get" << getUpperName() << "() << \""; - } } void writeDump(raw_ostream &OS) const override { @@ -320,9 +331,10 @@ namespace { OS << " OS << \" \";\n"; OS << " dumpBareDeclRef(SA->get" << getUpperName() << "());\n"; } else if (type == "IdentifierInfo *") { - if (isOptional()) - OS << " if (SA->get" << getUpperName() << "())\n "; - OS << " OS << \" \" << SA->get" << getUpperName() + // Some non-optional (comma required) identifier arguments can be the + // empty string but are then recorded as a nullptr. + OS << " if (SA->get" << getUpperName() << "())\n" + << " OS << \" \" << SA->get" << getUpperName() << "()->getName();\n"; } else if (type == "TypeSourceInfo *") { OS << " OS << \" \" << SA->get" << getUpperName() @@ -332,6 +344,11 @@ namespace { << getUpperName() << "\";\n"; } else if (type == "int" || type == "unsigned") { OS << " OS << \" \" << SA->get" << getUpperName() << "();\n"; + } else if (type == "ParamIdx") { + if (isOptional()) + OS << " if (SA->get" << getUpperName() << "().isValid())\n "; + OS << " OS << \" \" << SA->get" << getUpperName() + << "().getSourceIndex();\n"; } else { llvm_unreachable("Unknown SimpleArgument type!"); } @@ -574,12 +591,15 @@ namespace { << "Type());\n"; } + std::string getIsOmitted() const override { + return "!is" + getLowerName().str() + "Expr || !" + getLowerName().str() + + "Expr"; + } + void writeValue(raw_ostream &OS) const override { OS << "\";\n"; - // The aligned attribute argument expression is optional. - OS << " if (is" << getLowerName() << "Expr && " - << getLowerName() << "Expr)\n"; - OS << " " << getLowerName() << "Expr->printPretty(OS, nullptr, Policy);\n"; + OS << " " << getLowerName() + << "Expr->printPretty(OS, nullptr, Policy);\n"; OS << " OS << \""; } @@ -606,6 +626,10 @@ namespace { virtual void writeValueImpl(raw_ostream &OS) const { OS << " OS << Val;\n"; } + // Assumed to receive a parameter: raw_ostream OS. + virtual void writeDumpImpl(raw_ostream &OS) const { + OS << " OS << \" \" << Val;\n"; + } public: VariadicArgument(const Record &Arg, StringRef Attr, std::string T) @@ -732,7 +756,22 @@ namespace { void writeDump(raw_ostream &OS) const override { OS << " for (const auto &Val : SA->" << RangeName << "())\n"; - OS << " OS << \" \" << Val;\n"; + writeDumpImpl(OS); + } + }; + + class VariadicParamIdxArgument : public VariadicArgument { + public: + VariadicParamIdxArgument(const Record &Arg, StringRef Attr) + : VariadicArgument(Arg, Attr, "ParamIdx") {} + + public: + void writeValueImpl(raw_ostream &OS) const override { + OS << " OS << Val.getSourceIndex();\n"; + } + + void writeDumpImpl(raw_ostream &OS) const override { + OS << " OS << \" \" << Val.getSourceIndex();\n"; } }; @@ -1134,6 +1173,13 @@ namespace { } }; + class VariadicIdentifierArgument : public VariadicArgument { + public: + VariadicIdentifierArgument(const Record &Arg, StringRef Attr) + : VariadicArgument(Arg, Attr, "IdentifierInfo *") + {} + }; + class VariadicStringArgument : public VariadicArgument { public: VariadicStringArgument(const Record &Arg, StringRef Attr) @@ -1235,6 +1281,12 @@ createArgument(const Record &Arg, StringRef Attr, Ptr = llvm::make_unique<VariadicEnumArgument>(Arg, Attr); else if (ArgName == "VariadicExprArgument") Ptr = llvm::make_unique<VariadicExprArgument>(Arg, Attr); + else if (ArgName == "VariadicParamIdxArgument") + Ptr = llvm::make_unique<VariadicParamIdxArgument>(Arg, Attr); + else if (ArgName == "ParamIdxArgument") + Ptr = llvm::make_unique<SimpleArgument>(Arg, Attr, "ParamIdx"); + else if (ArgName == "VariadicIdentifierArgument") + Ptr = llvm::make_unique<VariadicIdentifierArgument>(Arg, Attr); else if (ArgName == "VersionArgument") Ptr = llvm::make_unique<VersionArgument>(Arg, Attr); @@ -1366,7 +1418,7 @@ writePrettyPrintFunction(Record &R, " OS << \"" << Prefix << Spelling; if (Variety == "Pragma") { - OS << " \";\n"; + OS << "\";\n"; OS << " printPrettyPragma(OS, Policy);\n"; OS << " OS << \"\\n\";"; OS << " break;\n"; @@ -1374,33 +1426,83 @@ writePrettyPrintFunction(Record &R, continue; } - // Fake arguments aren't part of the parsed form and should not be - // pretty-printed. - bool hasNonFakeArgs = llvm::any_of( - Args, [](const std::unique_ptr<Argument> &A) { return !A->isFake(); }); - - // FIXME: always printing the parenthesis isn't the correct behavior for - // attributes which have optional arguments that were not provided. For - // instance: __attribute__((aligned)) will be pretty printed as - // __attribute__((aligned())). The logic should check whether there is only - // a single argument, and if it is optional, whether it has been provided. - if (hasNonFakeArgs) - OS << "("; if (Spelling == "availability") { + OS << "("; writeAvailabilityValue(OS); + OS << ")"; } else if (Spelling == "deprecated" || Spelling == "gnu::deprecated") { - writeDeprecatedAttrValue(OS, Variety); + OS << "("; + writeDeprecatedAttrValue(OS, Variety); + OS << ")"; } else { - unsigned index = 0; + // To avoid printing parentheses around an empty argument list or + // printing spurious commas at the end of an argument list, we need to + // determine where the last provided non-fake argument is. + unsigned NonFakeArgs = 0; + unsigned TrailingOptArgs = 0; + bool FoundNonOptArg = false; + for (const auto &arg : llvm::reverse(Args)) { + if (arg->isFake()) + continue; + ++NonFakeArgs; + if (FoundNonOptArg) + continue; + // FIXME: arg->getIsOmitted() == "false" means we haven't implemented + // any way to detect whether the argument was omitted. + if (!arg->isOptional() || arg->getIsOmitted() == "false") { + FoundNonOptArg = true; + continue; + } + if (!TrailingOptArgs++) + OS << "\";\n" + << " unsigned TrailingOmittedArgs = 0;\n"; + OS << " if (" << arg->getIsOmitted() << ")\n" + << " ++TrailingOmittedArgs;\n"; + } + if (TrailingOptArgs) + OS << " OS << \""; + if (TrailingOptArgs < NonFakeArgs) + OS << "("; + else if (TrailingOptArgs) + OS << "\";\n" + << " if (TrailingOmittedArgs < " << NonFakeArgs << ")\n" + << " OS << \"(\";\n" + << " OS << \""; + unsigned ArgIndex = 0; for (const auto &arg : Args) { - if (arg->isFake()) continue; - if (index++) OS << ", "; + if (arg->isFake()) + continue; + if (ArgIndex) { + if (ArgIndex >= NonFakeArgs - TrailingOptArgs) + OS << "\";\n" + << " if (" << ArgIndex << " < " << NonFakeArgs + << " - TrailingOmittedArgs)\n" + << " OS << \", \";\n" + << " OS << \""; + else + OS << ", "; + } + std::string IsOmitted = arg->getIsOmitted(); + if (arg->isOptional() && IsOmitted != "false") + OS << "\";\n" + << " if (!(" << IsOmitted << ")) {\n" + << " OS << \""; arg->writeValue(OS); + if (arg->isOptional() && IsOmitted != "false") + OS << "\";\n" + << " }\n" + << " OS << \""; + ++ArgIndex; } + if (TrailingOptArgs < NonFakeArgs) + OS << ")"; + else if (TrailingOptArgs) + OS << "\";\n" + << " if (TrailingOmittedArgs < " << NonFakeArgs << ")\n" + << " OS << \")\";\n" + << " OS << \""; } - if (hasNonFakeArgs) - OS << ")"; OS << Suffix + "\";\n"; OS << @@ -1414,7 +1516,7 @@ writePrettyPrintFunction(Record &R, OS << "}\n\n"; } -/// \brief Return the index of a spelling in a spelling list. +/// Return the index of a spelling in a spelling list. static unsigned getSpellingListIndex(const std::vector<FlattenedSpelling> &SpellingList, const FlattenedSpelling &Spelling) { @@ -1963,7 +2065,7 @@ static void forEachUniqueSpelling(const Record &Attr, Fn &&F) { } } -/// \brief Emits the first-argument-is-type property for attributes. +/// Emits the first-argument-is-type property for attributes. static void emitClangAttrTypeArgList(RecordKeeper &Records, raw_ostream &OS) { OS << "#if defined(CLANG_ATTR_TYPE_ARG_LIST)\n"; std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr"); @@ -1985,7 +2087,7 @@ static void emitClangAttrTypeArgList(RecordKeeper &Records, raw_ostream &OS) { OS << "#endif // CLANG_ATTR_TYPE_ARG_LIST\n\n"; } -/// \brief Emits the parse-arguments-in-unevaluated-context property for +/// Emits the parse-arguments-in-unevaluated-context property for /// attributes. static void emitClangAttrArgContextList(RecordKeeper &Records, raw_ostream &OS) { OS << "#if defined(CLANG_ATTR_ARG_CONTEXT_LIST)\n"; @@ -2013,6 +2115,34 @@ static bool isIdentifierArgument(Record *Arg) { .Default(false); } +static bool isVariadicIdentifierArgument(Record *Arg) { + return !Arg->getSuperClasses().empty() && + llvm::StringSwitch<bool>( + Arg->getSuperClasses().back().first->getName()) + .Case("VariadicIdentifierArgument", true) + .Default(false); +} + +static void emitClangAttrVariadicIdentifierArgList(RecordKeeper &Records, + raw_ostream &OS) { + OS << "#if defined(CLANG_ATTR_VARIADIC_IDENTIFIER_ARG_LIST)\n"; + std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr"); + for (const auto *A : Attrs) { + // Determine whether the first argument is a variadic identifier. + std::vector<Record *> Args = A->getValueAsListOfDefs("Args"); + if (Args.empty() || !isVariadicIdentifierArgument(Args[0])) + continue; + + // All these spellings take an identifier argument. + forEachUniqueSpelling(*A, [&](const FlattenedSpelling &S) { + OS << ".Case(\"" << S.name() << "\", " + << "true" + << ")\n"; + }); + } + OS << "#endif // CLANG_ATTR_VARIADIC_IDENTIFIER_ARG_LIST\n\n"; +} + // Emits the first-argument-is-identifier property for attributes. static void emitClangAttrIdentifierArgList(RecordKeeper &Records, raw_ostream &OS) { OS << "#if defined(CLANG_ATTR_IDENTIFIER_ARG_LIST)\n"; @@ -2063,10 +2193,14 @@ void EmitClangAttrClass(RecordKeeper &Records, raw_ostream &OS) { ArrayRef<std::pair<Record *, SMRange>> Supers = R.getSuperClasses(); assert(!Supers.empty() && "Forgot to specify a superclass for the attr"); std::string SuperName; + bool Inheritable = false; for (const auto &Super : llvm::reverse(Supers)) { const Record *R = Super.first; - if (R->getName() != "TargetSpecificAttr" && SuperName.empty()) + if (R->getName() != "TargetSpecificAttr" && + R->getName() != "DeclOrTypeAttr" && SuperName.empty()) SuperName = R->getName(); + if (R->getName() == "InheritableAttr") + Inheritable = true; } OS << "class " << R.getName() << "Attr : public " << SuperName << " {\n"; @@ -2160,8 +2294,13 @@ void EmitClangAttrClass(RecordKeeper &Records, raw_ostream &OS) { OS << " )\n"; OS << " : " << SuperName << "(attr::" << R.getName() << ", R, SI, " - << ( R.getValueAsBit("LateParsed") ? "true" : "false" ) << ", " - << ( R.getValueAsBit("DuplicatesAllowedWhileMerging") ? "true" : "false" ) << ")\n"; + << ( R.getValueAsBit("LateParsed") ? "true" : "false" ); + if (Inheritable) { + OS << ", " + << (R.getValueAsBit("InheritEvenIfAlreadyPresent") ? "true" + : "false"); + } + OS << ")\n"; for (auto const &ai : Args) { OS << " , "; @@ -3030,7 +3169,7 @@ static void emitArgInfo(const Record &R, raw_ostream &OS) { } static void GenerateDefaultAppertainsTo(raw_ostream &OS) { - OS << "static bool defaultAppertainsTo(Sema &, const AttributeList &,"; + OS << "static bool defaultAppertainsTo(Sema &, const ParsedAttr &,"; OS << "const Decl *) {\n"; OS << " return true;\n"; OS << "}\n\n"; @@ -3168,7 +3307,7 @@ static std::string GenerateAppertainsTo(const Record &Attr, raw_ostream &OS) { // name of that check to the caller. std::string FnName = "check" + Attr.getName().str() + "AppertainsTo"; std::stringstream SS; - SS << "static bool " << FnName << "(Sema &S, const AttributeList &Attr, "; + SS << "static bool " << FnName << "(Sema &S, const ParsedAttr &Attr, "; SS << "const Decl *D) {\n"; SS << " if ("; for (auto I = Subjects.begin(), E = Subjects.end(); I != E; ++I) { @@ -3240,7 +3379,7 @@ emitAttributeMatchRules(PragmaClangAttributeSupport &PragmaAttributeSupport, static void GenerateDefaultLangOptRequirements(raw_ostream &OS) { OS << "static bool defaultDiagnoseLangOpts(Sema &, "; - OS << "const AttributeList &) {\n"; + OS << "const ParsedAttr &) {\n"; OS << " return true;\n"; OS << "}\n\n"; } @@ -3279,7 +3418,7 @@ static std::string GenerateLangOptRequirements(const Record &R, if (I != CustomLangOptsSet.end()) return *I; - OS << "static bool " << FnName << "(Sema &S, const AttributeList &Attr) {\n"; + OS << "static bool " << FnName << "(Sema &S, const ParsedAttr &Attr) {\n"; OS << " if (" << Test << ")\n"; OS << " return true;\n\n"; OS << " S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) "; @@ -3312,7 +3451,7 @@ static std::string GenerateTargetRequirements(const Record &Attr, // If there are other attributes which share the same parsed attribute kind, // such as target-specific attributes with a shared spelling, collapse the // duplicate architectures. This is required because a shared target-specific - // attribute has only one AttributeList::Kind enumeration value, but it + // attribute has only one ParsedAttr::Kind enumeration value, but it // applies to multiple target architectures. In order for the attribute to be // considered valid, all of its architectures need to be included. if (!Attr.isValueUnset("ParseKind")) { @@ -3349,7 +3488,7 @@ static std::string GenerateTargetRequirements(const Record &Attr, static void GenerateDefaultSpellingIndexToSemanticSpelling(raw_ostream &OS) { OS << "static unsigned defaultSpellingIndexToSemanticSpelling(" - << "const AttributeList &Attr) {\n"; + << "const ParsedAttr &Attr) {\n"; OS << " return UINT_MAX;\n"; OS << "}\n\n"; } @@ -3372,7 +3511,7 @@ static std::string GenerateSpellingIndexToSemanticSpelling(const Record &Attr, std::string Enum = CreateSemanticSpellings(Spellings, SemanticToSyntacticMap); std::string Name = Attr.getName().str() + "AttrSpellingMap"; - OS << "static unsigned " << Name << "(const AttributeList &Attr) {\n"; + OS << "static unsigned " << Name << "(const ParsedAttr &Attr) {\n"; OS << Enum; OS << " unsigned Idx = Attr.getAttributeSpellingListIndex();\n"; WriteSemanticSpellingSwitch("Idx", SemanticToSyntacticMap, OS); @@ -3422,12 +3561,14 @@ void EmitClangAttrParsedAttrImpl(RecordKeeper &Records, raw_ostream &OS) { // the spellings are identical, and custom parsing rules match, etc. // We need to generate struct instances based off ParsedAttrInfo from - // AttributeList.cpp. + // ParsedAttr.cpp. SS << " { "; emitArgInfo(*I->second, SS); SS << ", " << I->second->getValueAsBit("HasCustomParsing"); SS << ", " << I->second->isSubClassOf("TargetSpecificAttr"); - SS << ", " << I->second->isSubClassOf("TypeAttr"); + SS << ", " + << (I->second->isSubClassOf("TypeAttr") || + I->second->isSubClassOf("DeclOrTypeAttr")); SS << ", " << I->second->isSubClassOf("StmtAttr"); SS << ", " << IsKnownToGCC(*I->second); SS << ", " << PragmaAttributeSupport.isAttributedSupported(*I->second); @@ -3445,7 +3586,8 @@ void EmitClangAttrParsedAttrImpl(RecordKeeper &Records, raw_ostream &OS) { SS << " // AT_" << I->first << "\n"; } - OS << "static const ParsedAttrInfo AttrInfoMap[AttributeList::UnknownAttribute + 1] = {\n"; + OS << "static const ParsedAttrInfo AttrInfoMap[ParsedAttr::UnknownAttribute " + "+ 1] = {\n"; OS << SS.str(); OS << "};\n\n"; @@ -3473,7 +3615,7 @@ void EmitClangAttrParsedAttrKinds(RecordKeeper &Records, raw_ostream &OS) { // specific attribute, or MSP430-specific attribute. Additionally, an // attribute can be spelled GNU<"dllexport"> and Declspec<"dllexport"> // for the same semantic attribute. Ultimately, we need to map each of - // these to a single AttributeList::Kind value, but the StringMatcher + // these to a single ParsedAttr::Kind value, but the StringMatcher // class cannot handle duplicate match strings. So we generate a list of // string to match based on the syntax, and emit multiple string matchers // depending on the syntax used. @@ -3520,34 +3662,34 @@ void EmitClangAttrParsedAttrKinds(RecordKeeper &Records, raw_ostream &OS) { Spelling += RawSpelling; if (SemaHandler) - Matches->push_back(StringMatcher::StringPair(Spelling, - "return AttributeList::AT_" + AttrName + ";")); + Matches->push_back(StringMatcher::StringPair( + Spelling, "return ParsedAttr::AT_" + AttrName + ";")); else - Matches->push_back(StringMatcher::StringPair(Spelling, - "return AttributeList::IgnoredAttribute;")); + Matches->push_back(StringMatcher::StringPair( + Spelling, "return ParsedAttr::IgnoredAttribute;")); } } } - - OS << "static AttributeList::Kind getAttrKind(StringRef Name, "; - OS << "AttributeList::Syntax Syntax) {\n"; - OS << " if (AttributeList::AS_GNU == Syntax) {\n"; + + OS << "static ParsedAttr::Kind getAttrKind(StringRef Name, "; + OS << "ParsedAttr::Syntax Syntax) {\n"; + OS << " if (ParsedAttr::AS_GNU == Syntax) {\n"; StringMatcher("Name", GNU, OS).Emit(); - OS << " } else if (AttributeList::AS_Declspec == Syntax) {\n"; + OS << " } else if (ParsedAttr::AS_Declspec == Syntax) {\n"; StringMatcher("Name", Declspec, OS).Emit(); - OS << " } else if (AttributeList::AS_Microsoft == Syntax) {\n"; + OS << " } else if (ParsedAttr::AS_Microsoft == Syntax) {\n"; StringMatcher("Name", Microsoft, OS).Emit(); - OS << " } else if (AttributeList::AS_CXX11 == Syntax) {\n"; + OS << " } else if (ParsedAttr::AS_CXX11 == Syntax) {\n"; StringMatcher("Name", CXX11, OS).Emit(); - OS << " } else if (AttributeList::AS_C2x == Syntax) {\n"; + OS << " } else if (ParsedAttr::AS_C2x == Syntax) {\n"; StringMatcher("Name", C2x, OS).Emit(); - OS << " } else if (AttributeList::AS_Keyword == Syntax || "; - OS << "AttributeList::AS_ContextSensitiveKeyword == Syntax) {\n"; + OS << " } else if (ParsedAttr::AS_Keyword == Syntax || "; + OS << "ParsedAttr::AS_ContextSensitiveKeyword == Syntax) {\n"; StringMatcher("Name", Keywords, OS).Emit(); - OS << " } else if (AttributeList::AS_Pragma == Syntax) {\n"; + OS << " } else if (ParsedAttr::AS_Pragma == Syntax) {\n"; StringMatcher("Name", Pragma, OS).Emit(); OS << " }\n"; - OS << " return AttributeList::UnknownAttribute;\n" + OS << " return ParsedAttr::UnknownAttribute;\n" << "}\n"; } @@ -3592,6 +3734,7 @@ void EmitClangAttrParserStringSwitches(RecordKeeper &Records, emitSourceFileHeader("Parser-related llvm::StringSwitch cases", OS); emitClangAttrArgContextList(Records, OS); emitClangAttrIdentifierArgList(Records, OS); + emitClangAttrVariadicIdentifierArgList(Records, OS); emitClangAttrTypeArgList(Records, OS); emitClangAttrLateParsedList(Records, OS); } @@ -3753,8 +3896,8 @@ static void WriteDocumentation(RecordKeeper &Records, const Record &Deprecated = *Doc.Documentation->getValueAsDef("Deprecated"); const StringRef Replacement = Deprecated.getValueAsString("Replacement"); if (!Replacement.empty()) - OS << " This attribute has been superseded by ``" - << Replacement << "``."; + OS << " This attribute has been superseded by ``" << Replacement + << "``."; OS << "\n\n"; } @@ -3807,9 +3950,9 @@ void EmitClangAttrDocs(RecordKeeper &Records, raw_ostream &OS) { for (auto &I : SplitDocs) { WriteCategoryHeader(I.first, OS); - std::sort(I.second.begin(), I.second.end(), - [](const DocumentationData &D1, const DocumentationData &D2) { - return D1.Heading < D2.Heading; + llvm::sort(I.second.begin(), I.second.end(), + [](const DocumentationData &D1, const DocumentationData &D2) { + return D1.Heading < D2.Heading; }); // Walk over each of the attributes in the category and write out their diff --git a/utils/TableGen/ClangCommentHTMLNamedCharacterReferenceEmitter.cpp b/utils/TableGen/ClangCommentHTMLNamedCharacterReferenceEmitter.cpp index bfdb268b63ba..bea97ae13289 100644 --- a/utils/TableGen/ClangCommentHTMLNamedCharacterReferenceEmitter.cpp +++ b/utils/TableGen/ClangCommentHTMLNamedCharacterReferenceEmitter.cpp @@ -22,7 +22,7 @@ using namespace llvm; -/// \brief Convert a code point to the corresponding UTF-8 sequence represented +/// Convert a code point to the corresponding UTF-8 sequence represented /// as a C string literal. /// /// \returns true on success. diff --git a/utils/TableGen/ClangDiagnosticsEmitter.cpp b/utils/TableGen/ClangDiagnosticsEmitter.cpp index d9d99e0bb002..6bfb3f9f61f5 100644 --- a/utils/TableGen/ClangDiagnosticsEmitter.cpp +++ b/utils/TableGen/ClangDiagnosticsEmitter.cpp @@ -14,12 +14,13 @@ #include "llvm/ADT/DenseSet.h" #include "llvm/ADT/Optional.h" #include "llvm/ADT/PointerUnion.h" +#include "llvm/ADT/STLExtras.h" #include "llvm/ADT/SmallPtrSet.h" #include "llvm/ADT/SmallString.h" #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/StringMap.h" -#include "llvm/ADT/STLExtras.h" #include "llvm/ADT/Twine.h" +#include "llvm/Support/Casting.h" #include "llvm/TableGen/Error.h" #include "llvm/TableGen/Record.h" #include "llvm/TableGen/StringToOffsetTable.h" @@ -154,16 +155,7 @@ static bool beforeThanCompareGroups(const GroupInfo *LHS, const GroupInfo *RHS){ RHS->DiagsInGroup.front()); } -static SMRange findSuperClassRange(const Record *R, StringRef SuperName) { - ArrayRef<std::pair<Record *, SMRange>> Supers = R->getSuperClasses(); - auto I = std::find_if(Supers.begin(), Supers.end(), - [&](const std::pair<Record *, SMRange> &SuperPair) { - return SuperPair.first->getName() == SuperName; - }); - return (I != Supers.end()) ? I->second : SMRange(); -} - -/// \brief Invert the 1-[0/1] mapping of diags to group into a one to many +/// Invert the 1-[0/1] mapping of diags to group into a one to many /// mapping of groups to diags in the group. static void groupDiagnostics(const std::vector<Record*> &Diags, const std::vector<Record*> &DiagGroups, @@ -216,9 +208,9 @@ static void groupDiagnostics(const std::vector<Record*> &Diags, E = SortedGroups.end(); I != E; ++I) { MutableArrayRef<const Record *> GroupDiags = (*I)->DiagsInGroup; - std::sort(GroupDiags.begin(), GroupDiags.end(), beforeThanCompare); + llvm::sort(GroupDiags.begin(), GroupDiags.end(), beforeThanCompare); } - std::sort(SortedGroups.begin(), SortedGroups.end(), beforeThanCompareGroups); + llvm::sort(SortedGroups.begin(), SortedGroups.end(), beforeThanCompareGroups); // Warn about the same group being used anonymously in multiple places. for (SmallVectorImpl<GroupInfo *>::const_iterator I = SortedGroups.begin(), @@ -236,22 +228,10 @@ static void groupDiagnostics(const std::vector<Record*> &Diags, if (NextDiagGroup == (*I)->ExplicitDef) continue; - SMRange InGroupRange = findSuperClassRange(*DI, "InGroup"); - SmallString<64> Replacement; - if (InGroupRange.isValid()) { - Replacement += "InGroup<"; - Replacement += (*I)->ExplicitDef->getName(); - Replacement += ">"; - } - SMFixIt FixIt(InGroupRange, Replacement); - - SrcMgr.PrintMessage(NextDiagGroup->getLoc().front(), + SrcMgr.PrintMessage((*DI)->getLoc().front(), SourceMgr::DK_Error, Twine("group '") + Name + - "' is referred to anonymously", - None, - InGroupRange.isValid() ? FixIt - : ArrayRef<SMFixIt>()); + "' is referred to anonymously"); SrcMgr.PrintMessage((*I)->ExplicitDef->getLoc().front(), SourceMgr::DK_Note, "group defined here"); } @@ -266,19 +246,14 @@ static void groupDiagnostics(const std::vector<Record*> &Diags, const Record *NextDiagGroup = GroupInit->getDef(); std::string Name = NextDiagGroup->getValueAsString("GroupName"); - SMRange InGroupRange = findSuperClassRange(*DI, "InGroup"); - SrcMgr.PrintMessage(NextDiagGroup->getLoc().front(), + SrcMgr.PrintMessage((*DI)->getLoc().front(), SourceMgr::DK_Error, Twine("group '") + Name + - "' is referred to anonymously", - InGroupRange); + "' is referred to anonymously"); for (++DI; DI != DE; ++DI) { - GroupInit = cast<DefInit>((*DI)->getValueInit("Group")); - InGroupRange = findSuperClassRange(*DI, "InGroup"); - SrcMgr.PrintMessage(GroupInit->getDef()->getLoc().front(), - SourceMgr::DK_Note, "also referenced here", - InGroupRange); + SrcMgr.PrintMessage((*DI)->getLoc().front(), + SourceMgr::DK_Note, "also referenced here"); } } } @@ -467,6 +442,735 @@ void InferPedantic::compute(VecOrSet DiagsInPedantic, } } +namespace { +enum PieceKind { + MultiPieceClass, + TextPieceClass, + PlaceholderPieceClass, + SelectPieceClass, + PluralPieceClass, + DiffPieceClass, + SubstitutionPieceClass, +}; + +enum ModifierType { + MT_Unknown, + MT_Placeholder, + MT_Select, + MT_Sub, + MT_Plural, + MT_Diff, + MT_Ordinal, + MT_S, + MT_Q, + MT_ObjCClass, + MT_ObjCInstance, +}; + +static StringRef getModifierName(ModifierType MT) { + switch (MT) { + case MT_Select: + return "select"; + case MT_Sub: + return "sub"; + case MT_Diff: + return "diff"; + case MT_Plural: + return "plural"; + case MT_Ordinal: + return "ordinal"; + case MT_S: + return "s"; + case MT_Q: + return "q"; + case MT_Placeholder: + return ""; + case MT_ObjCClass: + return "objcclass"; + case MT_ObjCInstance: + return "objcinstance"; + case MT_Unknown: + llvm_unreachable("invalid modifier type"); + } + // Unhandled case + llvm_unreachable("invalid modifier type"); +} + +struct Piece { + // This type and its derived classes are move-only. + Piece(PieceKind Kind) : ClassKind(Kind) {} + Piece(Piece const &O) = delete; + Piece &operator=(Piece const &) = delete; + virtual ~Piece() {} + + PieceKind getPieceClass() const { return ClassKind; } + static bool classof(const Piece *) { return true; } + +private: + PieceKind ClassKind; +}; + +struct MultiPiece : Piece { + MultiPiece() : Piece(MultiPieceClass) {} + MultiPiece(std::vector<Piece *> Pieces) + : Piece(MultiPieceClass), Pieces(std::move(Pieces)) {} + + std::vector<Piece *> Pieces; + + static bool classof(const Piece *P) { + return P->getPieceClass() == MultiPieceClass; + } +}; + +struct TextPiece : Piece { + StringRef Role; + std::string Text; + TextPiece(StringRef Text, StringRef Role = "") + : Piece(TextPieceClass), Role(Role), Text(Text.str()) {} + + static bool classof(const Piece *P) { + return P->getPieceClass() == TextPieceClass; + } +}; + +struct PlaceholderPiece : Piece { + ModifierType Kind; + int Index; + PlaceholderPiece(ModifierType Kind, int Index) + : Piece(PlaceholderPieceClass), Kind(Kind), Index(Index) {} + + static bool classof(const Piece *P) { + return P->getPieceClass() == PlaceholderPieceClass; + } +}; + +struct SelectPiece : Piece { +protected: + SelectPiece(PieceKind Kind, ModifierType ModKind) + : Piece(Kind), ModKind(ModKind) {} + +public: + SelectPiece(ModifierType ModKind) : SelectPiece(SelectPieceClass, ModKind) {} + + ModifierType ModKind; + std::vector<Piece *> Options; + int Index; + + static bool classof(const Piece *P) { + return P->getPieceClass() == SelectPieceClass || + P->getPieceClass() == PluralPieceClass; + } +}; + +struct PluralPiece : SelectPiece { + PluralPiece() : SelectPiece(PluralPieceClass, MT_Plural) {} + + std::vector<Piece *> OptionPrefixes; + int Index; + + static bool classof(const Piece *P) { + return P->getPieceClass() == PluralPieceClass; + } +}; + +struct DiffPiece : Piece { + DiffPiece() : Piece(DiffPieceClass) {} + + Piece *Options[2] = {}; + int Indexes[2] = {}; + + static bool classof(const Piece *P) { + return P->getPieceClass() == DiffPieceClass; + } +}; + +struct SubstitutionPiece : Piece { + SubstitutionPiece() : Piece(SubstitutionPieceClass) {} + + std::string Name; + std::vector<int> Modifiers; + + static bool classof(const Piece *P) { + return P->getPieceClass() == SubstitutionPieceClass; + } +}; + +/// Diagnostic text, parsed into pieces. + + +struct DiagnosticTextBuilder { + DiagnosticTextBuilder(DiagnosticTextBuilder const &) = delete; + DiagnosticTextBuilder &operator=(DiagnosticTextBuilder const &) = delete; + + DiagnosticTextBuilder(RecordKeeper &Records) { + // Build up the list of substitution records. + for (auto *S : Records.getAllDerivedDefinitions("TextSubstitution")) { + EvaluatingRecordGuard Guard(&EvaluatingRecord, S); + Substitutions.try_emplace( + S->getName(), DiagText(*this, S->getValueAsString("Substitution"))); + } + + // Check that no diagnostic definitions have the same name as a + // substitution. + for (Record *Diag : Records.getAllDerivedDefinitions("Diagnostic")) { + StringRef Name = Diag->getName(); + if (Substitutions.count(Name)) + llvm::PrintFatalError( + Diag->getLoc(), + "Diagnostic '" + Name + + "' has same name as TextSubstitution definition"); + } + } + + std::vector<std::string> buildForDocumentation(StringRef Role, + const Record *R); + std::string buildForDefinition(const Record *R); + + Piece *getSubstitution(SubstitutionPiece *S) const { + auto It = Substitutions.find(S->Name); + if (It == Substitutions.end()) + PrintFatalError("Failed to find substitution with name: " + S->Name); + return It->second.Root; + } + + LLVM_ATTRIBUTE_NORETURN void PrintFatalError(llvm::Twine const &Msg) const { + assert(EvaluatingRecord && "not evaluating a record?"); + llvm::PrintFatalError(EvaluatingRecord->getLoc(), Msg); + } + +private: + struct DiagText { + DiagnosticTextBuilder &Builder; + std::vector<Piece *> AllocatedPieces; + Piece *Root = nullptr; + + template <class T, class... Args> T *New(Args &&... args) { + static_assert(std::is_base_of<Piece, T>::value, "must be piece"); + T *Mem = new T(std::forward<Args>(args)...); + AllocatedPieces.push_back(Mem); + return Mem; + } + + DiagText(DiagnosticTextBuilder &Builder, StringRef Text) + : Builder(Builder), Root(parseDiagText(Text)) {} + + Piece *parseDiagText(StringRef &Text, bool Nested = false); + int parseModifier(StringRef &) const; + + public: + DiagText(DiagText &&O) noexcept + : Builder(O.Builder), AllocatedPieces(std::move(O.AllocatedPieces)), + Root(O.Root) { + O.Root = nullptr; + } + + ~DiagText() { + for (Piece *P : AllocatedPieces) + delete P; + } + }; + +private: + const Record *EvaluatingRecord = nullptr; + struct EvaluatingRecordGuard { + EvaluatingRecordGuard(const Record **Dest, const Record *New) + : Dest(Dest), Old(*Dest) { + *Dest = New; + } + ~EvaluatingRecordGuard() { *Dest = Old; } + const Record **Dest; + const Record *Old; + }; + + StringMap<DiagText> Substitutions; +}; + +template <class Derived> struct DiagTextVisitor { + using ModifierMappingsType = Optional<std::vector<int>>; + +private: + Derived &getDerived() { return static_cast<Derived &>(*this); } + +public: + std::vector<int> + getSubstitutionMappings(SubstitutionPiece *P, + const ModifierMappingsType &Mappings) const { + std::vector<int> NewMappings; + for (int Idx : P->Modifiers) + NewMappings.push_back(mapIndex(Idx, Mappings)); + return NewMappings; + } + + struct SubstitutionContext { + SubstitutionContext(DiagTextVisitor &Visitor, SubstitutionPiece *P) + : Visitor(Visitor) { + Substitution = Visitor.Builder.getSubstitution(P); + OldMappings = std::move(Visitor.ModifierMappings); + std::vector<int> NewMappings = + Visitor.getSubstitutionMappings(P, OldMappings); + Visitor.ModifierMappings = std::move(NewMappings); + } + + ~SubstitutionContext() { + Visitor.ModifierMappings = std::move(OldMappings); + } + + private: + DiagTextVisitor &Visitor; + Optional<std::vector<int>> OldMappings; + + public: + Piece *Substitution; + }; + +public: + DiagTextVisitor(DiagnosticTextBuilder &Builder) : Builder(Builder) {} + + void Visit(Piece *P) { + switch (P->getPieceClass()) { +#define CASE(T) \ + case T##PieceClass: \ + return getDerived().Visit##T(static_cast<T##Piece *>(P)) + CASE(Multi); + CASE(Text); + CASE(Placeholder); + CASE(Select); + CASE(Plural); + CASE(Diff); + CASE(Substitution); +#undef CASE + } + } + + void VisitSubstitution(SubstitutionPiece *P) { + SubstitutionContext Guard(*this, P); + Visit(Guard.Substitution); + } + + int mapIndex(int Idx, + ModifierMappingsType const &ModifierMappings) const { + if (!ModifierMappings) + return Idx; + if (ModifierMappings->size() <= static_cast<unsigned>(Idx)) + Builder.PrintFatalError("Modifier value '" + std::to_string(Idx) + + "' is not valid for this mapping (has " + + std::to_string(ModifierMappings->size()) + + " mappings)"); + return (*ModifierMappings)[Idx]; + } + + int mapIndex(int Idx) const { + return mapIndex(Idx, ModifierMappings); + } + +protected: + DiagnosticTextBuilder &Builder; + ModifierMappingsType ModifierMappings; +}; + +void escapeRST(StringRef Str, std::string &Out) { + for (auto K : Str) { + if (StringRef("`*|_[]\\").count(K)) + Out.push_back('\\'); + Out.push_back(K); + } +} + +template <typename It> void padToSameLength(It Begin, It End) { + size_t Width = 0; + for (It I = Begin; I != End; ++I) + Width = std::max(Width, I->size()); + for (It I = Begin; I != End; ++I) + (*I) += std::string(Width - I->size(), ' '); +} + +template <typename It> void makeTableRows(It Begin, It End) { + if (Begin == End) + return; + padToSameLength(Begin, End); + for (It I = Begin; I != End; ++I) + *I = "|" + *I + "|"; +} + +void makeRowSeparator(std::string &Str) { + for (char &K : Str) + K = (K == '|' ? '+' : '-'); +} + +struct DiagTextDocPrinter : DiagTextVisitor<DiagTextDocPrinter> { + using BaseTy = DiagTextVisitor<DiagTextDocPrinter>; + DiagTextDocPrinter(DiagnosticTextBuilder &Builder, + std::vector<std::string> &RST) + : BaseTy(Builder), RST(RST) {} + + void gatherNodes( + Piece *OrigP, const ModifierMappingsType &CurrentMappings, + std::vector<std::pair<Piece *, ModifierMappingsType>> &Pieces) const { + if (auto *Sub = dyn_cast<SubstitutionPiece>(OrigP)) { + ModifierMappingsType NewMappings = + getSubstitutionMappings(Sub, CurrentMappings); + return gatherNodes(Builder.getSubstitution(Sub), NewMappings, Pieces); + } + if (auto *MD = dyn_cast<MultiPiece>(OrigP)) { + for (Piece *Node : MD->Pieces) + gatherNodes(Node, CurrentMappings, Pieces); + return; + } + Pieces.push_back(std::make_pair(OrigP, CurrentMappings)); + } + + void VisitMulti(MultiPiece *P) { + if (P->Pieces.empty()) { + RST.push_back(""); + return; + } + + if (P->Pieces.size() == 1) + return Visit(P->Pieces[0]); + + // Flatten the list of nodes, replacing any substitution pieces with the + // recursively flattened substituted node. + std::vector<std::pair<Piece *, ModifierMappingsType>> Pieces; + gatherNodes(P, ModifierMappings, Pieces); + + std::string EmptyLinePrefix; + size_t Start = RST.size(); + bool HasMultipleLines = true; + for (const std::pair<Piece *, ModifierMappingsType> &NodePair : Pieces) { + std::vector<std::string> Lines; + DiagTextDocPrinter Visitor{Builder, Lines}; + Visitor.ModifierMappings = NodePair.second; + Visitor.Visit(NodePair.first); + + if (Lines.empty()) + continue; + + // We need a vertical separator if either this or the previous piece is a + // multi-line piece, or this is the last piece. + const char *Separator = (Lines.size() > 1 || HasMultipleLines) ? "|" : ""; + HasMultipleLines = Lines.size() > 1; + + if (Start + Lines.size() > RST.size()) + RST.resize(Start + Lines.size(), EmptyLinePrefix); + + padToSameLength(Lines.begin(), Lines.end()); + for (size_t I = 0; I != Lines.size(); ++I) + RST[Start + I] += Separator + Lines[I]; + std::string Empty(Lines[0].size(), ' '); + for (size_t I = Start + Lines.size(); I != RST.size(); ++I) + RST[I] += Separator + Empty; + EmptyLinePrefix += Separator + Empty; + } + for (size_t I = Start; I != RST.size(); ++I) + RST[I] += "|"; + EmptyLinePrefix += "|"; + + makeRowSeparator(EmptyLinePrefix); + RST.insert(RST.begin() + Start, EmptyLinePrefix); + RST.insert(RST.end(), EmptyLinePrefix); + } + + void VisitText(TextPiece *P) { + RST.push_back(""); + auto &S = RST.back(); + + StringRef T = P->Text; + while (!T.empty() && T.front() == ' ') { + RST.back() += " |nbsp| "; + T = T.drop_front(); + } + + std::string Suffix; + while (!T.empty() && T.back() == ' ') { + Suffix += " |nbsp| "; + T = T.drop_back(); + } + + if (!T.empty()) { + S += ':'; + S += P->Role; + S += ":`"; + escapeRST(T, S); + S += '`'; + } + + S += Suffix; + } + + void VisitPlaceholder(PlaceholderPiece *P) { + RST.push_back(std::string(":placeholder:`") + + char('A' + mapIndex(P->Index)) + "`"); + } + + void VisitSelect(SelectPiece *P) { + std::vector<size_t> SeparatorIndexes; + SeparatorIndexes.push_back(RST.size()); + RST.emplace_back(); + for (auto *O : P->Options) { + Visit(O); + SeparatorIndexes.push_back(RST.size()); + RST.emplace_back(); + } + + makeTableRows(RST.begin() + SeparatorIndexes.front(), + RST.begin() + SeparatorIndexes.back() + 1); + for (size_t I : SeparatorIndexes) + makeRowSeparator(RST[I]); + } + + void VisitPlural(PluralPiece *P) { VisitSelect(P); } + + void VisitDiff(DiffPiece *P) { Visit(P->Options[1]); } + + std::vector<std::string> &RST; +}; + +struct DiagTextPrinter : DiagTextVisitor<DiagTextPrinter> { +public: + using BaseTy = DiagTextVisitor<DiagTextPrinter>; + DiagTextPrinter(DiagnosticTextBuilder &Builder, std::string &Result) + : BaseTy(Builder), Result(Result) {} + + void VisitMulti(MultiPiece *P) { + for (auto *Child : P->Pieces) + Visit(Child); + } + void VisitText(TextPiece *P) { Result += P->Text; } + void VisitPlaceholder(PlaceholderPiece *P) { + Result += "%"; + Result += getModifierName(P->Kind); + addInt(mapIndex(P->Index)); + } + void VisitSelect(SelectPiece *P) { + Result += "%"; + Result += getModifierName(P->ModKind); + if (P->ModKind == MT_Select) { + Result += "{"; + for (auto *D : P->Options) { + Visit(D); + Result += '|'; + } + if (!P->Options.empty()) + Result.erase(--Result.end()); + Result += '}'; + } + addInt(mapIndex(P->Index)); + } + + void VisitPlural(PluralPiece *P) { + Result += "%plural{"; + assert(P->Options.size() == P->OptionPrefixes.size()); + for (unsigned I = 0, End = P->Options.size(); I < End; ++I) { + if (P->OptionPrefixes[I]) + Visit(P->OptionPrefixes[I]); + Visit(P->Options[I]); + Result += "|"; + } + if (!P->Options.empty()) + Result.erase(--Result.end()); + Result += '}'; + addInt(mapIndex(P->Index)); + } + + void VisitDiff(DiffPiece *P) { + Result += "%diff{"; + Visit(P->Options[0]); + Result += "|"; + Visit(P->Options[1]); + Result += "}"; + addInt(mapIndex(P->Indexes[0])); + Result += ","; + addInt(mapIndex(P->Indexes[1])); + } + + void addInt(int Val) { Result += std::to_string(Val); } + + std::string &Result; +}; + +int DiagnosticTextBuilder::DiagText::parseModifier(StringRef &Text) const { + if (Text.empty() || !isdigit(Text[0])) + Builder.PrintFatalError("expected modifier in diagnostic"); + int Val = 0; + do { + Val *= 10; + Val += Text[0] - '0'; + Text = Text.drop_front(); + } while (!Text.empty() && isdigit(Text[0])); + return Val; +} + +Piece *DiagnosticTextBuilder::DiagText::parseDiagText(StringRef &Text, + bool Nested) { + std::vector<Piece *> Parsed; + + while (!Text.empty()) { + size_t End = (size_t)-2; + do + End = Nested ? Text.find_first_of("%|}", End + 2) + : Text.find_first_of('%', End + 2); + while (End < Text.size() - 1 && Text[End] == '%' && + (Text[End + 1] == '%' || Text[End + 1] == '|')); + + if (End) { + Parsed.push_back(New<TextPiece>(Text.slice(0, End), "diagtext")); + Text = Text.slice(End, StringRef::npos); + if (Text.empty()) + break; + } + + if (Text[0] == '|' || Text[0] == '}') + break; + + // Drop the '%'. + Text = Text.drop_front(); + + // Extract the (optional) modifier. + size_t ModLength = Text.find_first_of("0123456789{"); + StringRef Modifier = Text.slice(0, ModLength); + Text = Text.slice(ModLength, StringRef::npos); + ModifierType ModType = llvm::StringSwitch<ModifierType>{Modifier} + .Case("select", MT_Select) + .Case("sub", MT_Sub) + .Case("diff", MT_Diff) + .Case("plural", MT_Plural) + .Case("s", MT_S) + .Case("ordinal", MT_Ordinal) + .Case("q", MT_Q) + .Case("objcclass", MT_ObjCClass) + .Case("objcinstance", MT_ObjCInstance) + .Case("", MT_Placeholder) + .Default(MT_Unknown); + + switch (ModType) { + case MT_Unknown: + Builder.PrintFatalError("Unknown modifier type: " + Modifier); + case MT_Select: { + SelectPiece *Select = New<SelectPiece>(MT_Select); + do { + Text = Text.drop_front(); // '{' or '|' + Select->Options.push_back(parseDiagText(Text, true)); + assert(!Text.empty() && "malformed %select"); + } while (Text.front() == '|'); + // Drop the trailing '}'. + Text = Text.drop_front(1); + Select->Index = parseModifier(Text); + Parsed.push_back(Select); + continue; + } + case MT_Plural: { + PluralPiece *Plural = New<PluralPiece>(); + do { + Text = Text.drop_front(); // '{' or '|' + size_t End = Text.find_first_of(":"); + if (End == StringRef::npos) + Builder.PrintFatalError("expected ':' while parsing %plural"); + ++End; + assert(!Text.empty()); + Plural->OptionPrefixes.push_back( + New<TextPiece>(Text.slice(0, End), "diagtext")); + Text = Text.slice(End, StringRef::npos); + Plural->Options.push_back(parseDiagText(Text, true)); + assert(!Text.empty() && "malformed %select"); + } while (Text.front() == '|'); + // Drop the trailing '}'. + Text = Text.drop_front(1); + Plural->Index = parseModifier(Text); + Parsed.push_back(Plural); + continue; + } + case MT_Sub: { + SubstitutionPiece *Sub = New<SubstitutionPiece>(); + Text = Text.drop_front(); // '{' + size_t NameSize = Text.find_first_of('}'); + assert(NameSize != size_t(-1) && "failed to find the end of the name"); + assert(NameSize != 0 && "empty name?"); + Sub->Name = Text.substr(0, NameSize).str(); + Text = Text.drop_front(NameSize); + Text = Text.drop_front(); // '}' + if (!Text.empty()) { + while (true) { + if (!isdigit(Text[0])) + break; + Sub->Modifiers.push_back(parseModifier(Text)); + if (Text.empty() || Text[0] != ',') + break; + Text = Text.drop_front(); // ',' + assert(!Text.empty() && isdigit(Text[0]) && + "expected another modifier"); + } + } + Parsed.push_back(Sub); + continue; + } + case MT_Diff: { + DiffPiece *Diff = New<DiffPiece>(); + Text = Text.drop_front(); // '{' + Diff->Options[0] = parseDiagText(Text, true); + Text = Text.drop_front(); // '|' + Diff->Options[1] = parseDiagText(Text, true); + + Text = Text.drop_front(); // '}' + Diff->Indexes[0] = parseModifier(Text); + Text = Text.drop_front(); // ',' + Diff->Indexes[1] = parseModifier(Text); + Parsed.push_back(Diff); + continue; + } + case MT_S: { + SelectPiece *Select = New<SelectPiece>(ModType); + Select->Options.push_back(New<TextPiece>("")); + Select->Options.push_back(New<TextPiece>("s", "diagtext")); + Select->Index = parseModifier(Text); + Parsed.push_back(Select); + continue; + } + case MT_Q: + case MT_Placeholder: + case MT_ObjCClass: + case MT_ObjCInstance: + case MT_Ordinal: { + Parsed.push_back(New<PlaceholderPiece>(ModType, parseModifier(Text))); + continue; + } + } + } + + return New<MultiPiece>(Parsed); +} + +std::vector<std::string> +DiagnosticTextBuilder::buildForDocumentation(StringRef Severity, + const Record *R) { + EvaluatingRecordGuard Guard(&EvaluatingRecord, R); + StringRef Text = R->getValueAsString("Text"); + + DiagText D(*this, Text); + TextPiece *Prefix = D.New<TextPiece>(Severity, Severity); + Prefix->Text += ": "; + auto *MP = dyn_cast<MultiPiece>(D.Root); + if (!MP) { + MP = D.New<MultiPiece>(); + MP->Pieces.push_back(D.Root); + D.Root = MP; + } + MP->Pieces.insert(MP->Pieces.begin(), Prefix); + std::vector<std::string> Result; + DiagTextDocPrinter{*this, Result}.Visit(D.Root); + return Result; +} + +std::string DiagnosticTextBuilder::buildForDefinition(const Record *R) { + EvaluatingRecordGuard Guard(&EvaluatingRecord, R); + StringRef Text = R->getValueAsString("Text"); + DiagText D(*this, Text); + std::string Result; + DiagTextPrinter{*this, Result}.Visit(D.Root); + return Result; +} + +} // namespace + //===----------------------------------------------------------------------===// // Warning Tables (.inc file) generation. //===----------------------------------------------------------------------===// @@ -481,6 +1185,7 @@ static bool isRemark(const Record &Diag) { return ClsName == "CLASS_REMARK"; } + /// ClangDiagsDefsEmitter - The top-level class emits .def files containing /// declarations of Clang diagnostics. namespace clang { @@ -496,8 +1201,9 @@ void EmitClangDiagsDefs(RecordKeeper &Records, raw_ostream &OS, OS << "#endif\n\n"; } - const std::vector<Record*> &Diags = - Records.getAllDerivedDefinitions("Diagnostic"); + DiagnosticTextBuilder DiagTextBuilder(Records); + + std::vector<Record *> Diags = Records.getAllDerivedDefinitions("Diagnostic"); std::vector<Record*> DiagGroups = Records.getAllDerivedDefinitions("DiagGroup"); @@ -546,7 +1252,7 @@ void EmitClangDiagsDefs(RecordKeeper &Records, raw_ostream &OS, // Description string. OS << ", \""; - OS.write_escaped(R.getValueAsString("Text")) << '"'; + OS.write_escaped(DiagTextBuilder.buildForDefinition(&R)) << '"'; // Warning associated with the diagnostic. This is stored as an index into // the alphabetically sorted warning table. @@ -598,7 +1304,7 @@ static std::string getDiagCategoryEnum(llvm::StringRef name) { return enumName.str(); } -/// \brief Emit the array of diagnostic subgroups. +/// Emit the array of diagnostic subgroups. /// /// The array of diagnostic subgroups contains for each group a list of its /// subgroups. The individual lists are separated by '-1'. Groups with no @@ -645,7 +1351,7 @@ static void emitDiagSubGroups(std::map<std::string, GroupInfo> &DiagsInGroup, OS << "};\n\n"; } -/// \brief Emit the list of diagnostic arrays. +/// Emit the list of diagnostic arrays. /// /// This data structure is a large array that contains itself arrays of varying /// size. Each array represents a list of diagnostics. The different arrays are @@ -686,7 +1392,7 @@ static void emitDiagArrays(std::map<std::string, GroupInfo> &DiagsInGroup, OS << "};\n\n"; } -/// \brief Emit a list of group names. +/// Emit a list of group names. /// /// This creates a long string which by itself contains a list of pascal style /// strings, which consist of a length byte directly followed by the string. @@ -703,7 +1409,7 @@ static void emitDiagGroupNames(StringToOffsetTable &GroupNames, OS << "};\n\n"; } -/// \brief Emit diagnostic arrays and related data structures. +/// Emit diagnostic arrays and related data structures. /// /// This creates the actual diagnostic array, an array of diagnostic subgroups /// and an array of subgroup names. @@ -727,7 +1433,7 @@ static void emitAllDiagArrays(std::map<std::string, GroupInfo> &DiagsInGroup, OS << "#endif // GET_DIAG_ARRAYS\n\n"; } -/// \brief Emit diagnostic table. +/// Emit diagnostic table. /// /// The table is sorted by the name of the diagnostic group. Each element /// consists of the name of the diagnostic group (given as offset in the @@ -802,7 +1508,7 @@ static void emitDiagTable(std::map<std::string, GroupInfo> &DiagsInGroup, OS << "#endif // GET_DIAG_TABLE\n\n"; } -/// \brief Emit the table of diagnostic categories. +/// Emit the table of diagnostic categories. /// /// The table has the form of macro calls that have two parameters. The /// category's name as well as an enum that represents the category. The @@ -889,9 +1595,10 @@ void EmitClangDiagsIndexName(RecordKeeper &Records, raw_ostream &OS) { Index.push_back(RecordIndexElement(R)); } - std::sort(Index.begin(), Index.end(), - [](const RecordIndexElement &Lhs, - const RecordIndexElement &Rhs) { return Lhs.Name < Rhs.Name; }); + llvm::sort(Index.begin(), Index.end(), + [](const RecordIndexElement &Lhs, const RecordIndexElement &Rhs) { + return Lhs.Name < Rhs.Name; + }); for (unsigned i = 0, e = Index.size(); i != e; ++i) { const RecordIndexElement &R = Index[i]; @@ -907,261 +1614,6 @@ void EmitClangDiagsIndexName(RecordKeeper &Records, raw_ostream &OS) { namespace docs { namespace { -/// Diagnostic text, parsed into pieces. -struct DiagText { - struct Piece { - // This type and its derived classes are move-only. - Piece() {} - Piece(Piece &&O) {} - Piece &operator=(Piece &&O) { return *this; } - - virtual void print(std::vector<std::string> &RST) = 0; - virtual ~Piece() {} - }; - struct TextPiece : Piece { - StringRef Role; - std::string Text; - void print(std::vector<std::string> &RST) override; - }; - struct PlaceholderPiece : Piece { - int Index; - void print(std::vector<std::string> &RST) override; - }; - struct SelectPiece : Piece { - SelectPiece() {} - SelectPiece(SelectPiece &&O) noexcept : Options(std::move(O.Options)) {} - std::vector<DiagText> Options; - void print(std::vector<std::string> &RST) override; - }; - - std::vector<std::unique_ptr<Piece>> Pieces; - - DiagText(); - DiagText(DiagText &&O) noexcept : Pieces(std::move(O.Pieces)) {} - - DiagText(StringRef Text); - DiagText(StringRef Kind, StringRef Text); - - template<typename P> void add(P Piece) { - Pieces.push_back(llvm::make_unique<P>(std::move(Piece))); - } - void print(std::vector<std::string> &RST); -}; - -DiagText parseDiagText(StringRef &Text, bool Nested = false) { - DiagText Parsed; - - while (!Text.empty()) { - size_t End = (size_t)-2; - do - End = Nested ? Text.find_first_of("%|}", End + 2) - : Text.find_first_of('%', End + 2); - while (End < Text.size() - 1 && Text[End] == '%' && Text[End + 1] == '%'); - - if (End) { - DiagText::TextPiece Piece; - Piece.Role = "diagtext"; - Piece.Text = Text.slice(0, End); - Parsed.add(std::move(Piece)); - Text = Text.slice(End, StringRef::npos); - if (Text.empty()) break; - } - - if (Text[0] == '|' || Text[0] == '}') - break; - - // Drop the '%'. - Text = Text.drop_front(); - - // Extract the (optional) modifier. - size_t ModLength = Text.find_first_of("0123456789{"); - StringRef Modifier = Text.slice(0, ModLength); - Text = Text.slice(ModLength, StringRef::npos); - - // FIXME: Handle %ordinal here. - if (Modifier == "select" || Modifier == "plural") { - DiagText::SelectPiece Select; - do { - Text = Text.drop_front(); - if (Modifier == "plural") - while (Text[0] != ':') - Text = Text.drop_front(); - Select.Options.push_back(parseDiagText(Text, true)); - assert(!Text.empty() && "malformed %select"); - } while (Text.front() == '|'); - Parsed.add(std::move(Select)); - - // Drop the trailing '}n'. - Text = Text.drop_front(2); - continue; - } - - // For %diff, just take the second alternative (tree diagnostic). It would - // be preferable to take the first one, and replace the $ with the suitable - // placeholders. - if (Modifier == "diff") { - Text = Text.drop_front(); // '{' - parseDiagText(Text, true); - Text = Text.drop_front(); // '|' - - DiagText D = parseDiagText(Text, true); - for (auto &P : D.Pieces) - Parsed.Pieces.push_back(std::move(P)); - - Text = Text.drop_front(4); // '}n,m' - continue; - } - - if (Modifier == "s") { - Text = Text.drop_front(); - DiagText::SelectPiece Select; - Select.Options.push_back(DiagText("")); - Select.Options.push_back(DiagText("s")); - Parsed.add(std::move(Select)); - continue; - } - - assert(!Text.empty() && isdigit(Text[0]) && "malformed placeholder"); - DiagText::PlaceholderPiece Placeholder; - Placeholder.Index = Text[0] - '0'; - Parsed.add(std::move(Placeholder)); - Text = Text.drop_front(); - continue; - } - return Parsed; -} - -DiagText::DiagText() {} - -DiagText::DiagText(StringRef Text) : DiagText(parseDiagText(Text, false)) {} - -DiagText::DiagText(StringRef Kind, StringRef Text) : DiagText(parseDiagText(Text, false)) { - TextPiece Prefix; - Prefix.Role = Kind; - Prefix.Text = Kind; - Prefix.Text += ": "; - Pieces.insert(Pieces.begin(), - llvm::make_unique<TextPiece>(std::move(Prefix))); -} - -void escapeRST(StringRef Str, std::string &Out) { - for (auto K : Str) { - if (StringRef("`*|_[]\\").count(K)) - Out.push_back('\\'); - Out.push_back(K); - } -} - -template<typename It> void padToSameLength(It Begin, It End) { - size_t Width = 0; - for (It I = Begin; I != End; ++I) - Width = std::max(Width, I->size()); - for (It I = Begin; I != End; ++I) - (*I) += std::string(Width - I->size(), ' '); -} - -template<typename It> void makeTableRows(It Begin, It End) { - if (Begin == End) return; - padToSameLength(Begin, End); - for (It I = Begin; I != End; ++I) - *I = "|" + *I + "|"; -} - -void makeRowSeparator(std::string &Str) { - for (char &K : Str) - K = (K == '|' ? '+' : '-'); -} - -void DiagText::print(std::vector<std::string> &RST) { - if (Pieces.empty()) { - RST.push_back(""); - return; - } - - if (Pieces.size() == 1) - return Pieces[0]->print(RST); - - std::string EmptyLinePrefix; - size_t Start = RST.size(); - bool HasMultipleLines = true; - for (auto &P : Pieces) { - std::vector<std::string> Lines; - P->print(Lines); - if (Lines.empty()) - continue; - - // We need a vertical separator if either this or the previous piece is a - // multi-line piece, or this is the last piece. - const char *Separator = (Lines.size() > 1 || HasMultipleLines) ? "|" : ""; - HasMultipleLines = Lines.size() > 1; - - if (Start + Lines.size() > RST.size()) - RST.resize(Start + Lines.size(), EmptyLinePrefix); - - padToSameLength(Lines.begin(), Lines.end()); - for (size_t I = 0; I != Lines.size(); ++I) - RST[Start + I] += Separator + Lines[I]; - std::string Empty(Lines[0].size(), ' '); - for (size_t I = Start + Lines.size(); I != RST.size(); ++I) - RST[I] += Separator + Empty; - EmptyLinePrefix += Separator + Empty; - } - for (size_t I = Start; I != RST.size(); ++I) - RST[I] += "|"; - EmptyLinePrefix += "|"; - - makeRowSeparator(EmptyLinePrefix); - RST.insert(RST.begin() + Start, EmptyLinePrefix); - RST.insert(RST.end(), EmptyLinePrefix); -} - -void DiagText::TextPiece::print(std::vector<std::string> &RST) { - RST.push_back(""); - auto &S = RST.back(); - - StringRef T = Text; - while (!T.empty() && T.front() == ' ') { - RST.back() += " |nbsp| "; - T = T.drop_front(); - } - - std::string Suffix; - while (!T.empty() && T.back() == ' ') { - Suffix += " |nbsp| "; - T = T.drop_back(); - } - - if (!T.empty()) { - S += ':'; - S += Role; - S += ":`"; - escapeRST(T, S); - S += '`'; - } - - S += Suffix; -} - -void DiagText::PlaceholderPiece::print(std::vector<std::string> &RST) { - RST.push_back(std::string(":placeholder:`") + char('A' + Index) + "`"); -} - -void DiagText::SelectPiece::print(std::vector<std::string> &RST) { - std::vector<size_t> SeparatorIndexes; - SeparatorIndexes.push_back(RST.size()); - RST.emplace_back(); - for (auto &O : Options) { - O.print(RST); - SeparatorIndexes.push_back(RST.size()); - RST.emplace_back(); - } - - makeTableRows(RST.begin() + SeparatorIndexes.front(), - RST.begin() + SeparatorIndexes.back() + 1); - for (size_t I : SeparatorIndexes) - makeRowSeparator(RST[I]); -} - bool isRemarkGroup(const Record *DiagGroup, const std::map<std::string, GroupInfo> &DiagsInGroup) { bool AnyRemarks = false, AnyNonRemarks = false; @@ -1206,12 +1658,13 @@ void writeHeader(StringRef Str, raw_ostream &OS, char Kind = '-') { OS << Str << "\n" << std::string(Str.size(), Kind) << "\n"; } -void writeDiagnosticText(StringRef Role, StringRef Text, raw_ostream &OS) { +void writeDiagnosticText(DiagnosticTextBuilder &Builder, const Record *R, + StringRef Role, raw_ostream &OS) { + StringRef Text = R->getValueAsString("Text"); if (Text == "%0") OS << "The text of this diagnostic is not controlled by Clang.\n\n"; else { - std::vector<std::string> Out; - DiagText(Role, Text).print(Out); + std::vector<std::string> Out = Builder.buildForDocumentation(Role, R); for (auto &Line : Out) OS << Line << "\n"; OS << "\n"; @@ -1234,11 +1687,14 @@ void EmitClangDiagDocs(RecordKeeper &Records, raw_ostream &OS) { OS << Documentation->getValueAsString("Intro") << "\n"; + DiagnosticTextBuilder Builder(Records); + std::vector<Record*> Diags = Records.getAllDerivedDefinitions("Diagnostic"); + std::vector<Record*> DiagGroups = Records.getAllDerivedDefinitions("DiagGroup"); - std::sort(DiagGroups.begin(), DiagGroups.end(), diagGroupBeforeByName); + llvm::sort(DiagGroups.begin(), DiagGroups.end(), diagGroupBeforeByName); DiagGroupParentMap DGParentMap(Records); @@ -1257,10 +1713,10 @@ void EmitClangDiagDocs(RecordKeeper &Records, raw_ostream &OS) { DiagsInPedanticSet.end()); RecordVec GroupsInPedantic(GroupsInPedanticSet.begin(), GroupsInPedanticSet.end()); - std::sort(DiagsInPedantic.begin(), DiagsInPedantic.end(), - beforeThanCompare); - std::sort(GroupsInPedantic.begin(), GroupsInPedantic.end(), - beforeThanCompare); + llvm::sort(DiagsInPedantic.begin(), DiagsInPedantic.end(), + beforeThanCompare); + llvm::sort(GroupsInPedantic.begin(), GroupsInPedantic.end(), + beforeThanCompare); PedDiags.DiagsInGroup.insert(PedDiags.DiagsInGroup.end(), DiagsInPedantic.begin(), DiagsInPedantic.end()); @@ -1309,7 +1765,7 @@ void EmitClangDiagDocs(RecordKeeper &Records, raw_ostream &OS) { OS << "Also controls "; bool First = true; - std::sort(GroupInfo.SubGroups.begin(), GroupInfo.SubGroups.end()); + llvm::sort(GroupInfo.SubGroups.begin(), GroupInfo.SubGroups.end()); for (const auto &Name : GroupInfo.SubGroups) { if (!First) OS << ", "; OS << "`" << (IsRemarkGroup ? "-R" : "-W") << Name << "`_"; @@ -1325,7 +1781,8 @@ void EmitClangDiagDocs(RecordKeeper &Records, raw_ostream &OS) { Severity[0] = tolower(Severity[0]); if (Severity == "ignored") Severity = IsRemarkGroup ? "remark" : "warning"; - writeDiagnosticText(Severity, D->getValueAsString("Text"), OS); + + writeDiagnosticText(Builder, D, Severity, OS); } } diff --git a/utils/TableGen/ClangOptionDocEmitter.cpp b/utils/TableGen/ClangOptionDocEmitter.cpp index 59314510e0ad..7fe487e54698 100644 --- a/utils/TableGen/ClangOptionDocEmitter.cpp +++ b/utils/TableGen/ClangOptionDocEmitter.cpp @@ -111,7 +111,7 @@ Documentation extractDocumentation(RecordKeeper &Records) { auto DocumentationForOption = [&](Record *R) -> DocumentedOption { auto &A = Aliases[R]; - std::sort(A.begin(), A.end(), CompareByName); + llvm::sort(A.begin(), A.end(), CompareByName); return {R, std::move(A)}; }; @@ -120,7 +120,7 @@ Documentation extractDocumentation(RecordKeeper &Records) { Documentation D; auto &Groups = GroupsInGroup[R]; - std::sort(Groups.begin(), Groups.end(), CompareByLocation); + llvm::sort(Groups.begin(), Groups.end(), CompareByLocation); for (Record *G : Groups) { D.Groups.emplace_back(); D.Groups.back().Group = G; @@ -129,7 +129,7 @@ Documentation extractDocumentation(RecordKeeper &Records) { } auto &Options = OptionsInGroup[R]; - std::sort(Options.begin(), Options.end(), CompareByName); + llvm::sort(Options.begin(), Options.end(), CompareByName); for (Record *O : Options) D.Options.push_back(DocumentationForOption(O)); @@ -245,19 +245,27 @@ void emitOptionWithArgs(StringRef Prefix, const Record *Option, void emitOptionName(StringRef Prefix, const Record *Option, raw_ostream &OS) { // Find the arguments to list after the option. unsigned NumArgs = getNumArgsForKind(Option->getValueAsDef("Kind"), Option); + bool HasMetaVarName = !Option->isValueUnset("MetaVarName"); std::vector<std::string> Args; - if (!Option->isValueUnset("MetaVarName")) + if (HasMetaVarName) Args.push_back(Option->getValueAsString("MetaVarName")); else if (NumArgs == 1) Args.push_back("<arg>"); - while (Args.size() < NumArgs) { - Args.push_back(("<arg" + Twine(Args.size() + 1) + ">").str()); - // Use '--args <arg1> <arg2>...' if any number of args are allowed. - if (Args.size() == 2 && NumArgs == UnlimitedArgs) { - Args.back() += "..."; - break; + // Fill up arguments if this option didn't provide a meta var name or it + // supports an unlimited number of arguments. We can't see how many arguments + // already are in a meta var name, so assume it has right number. This is + // needed for JoinedAndSeparate options so that there arent't too many + // arguments. + if (!HasMetaVarName || NumArgs == UnlimitedArgs) { + while (Args.size() < NumArgs) { + Args.push_back(("<arg" + Twine(Args.size() + 1) + ">").str()); + // Use '--args <arg1> <arg2>...' if any number of args are allowed. + if (Args.size() == 2 && NumArgs == UnlimitedArgs) { + Args.back() += "..."; + break; + } } } diff --git a/utils/TableGen/ClangSACheckersEmitter.cpp b/utils/TableGen/ClangSACheckersEmitter.cpp index 8f3de0b67d77..4fda8e47e4a8 100644 --- a/utils/TableGen/ClangSACheckersEmitter.cpp +++ b/utils/TableGen/ClangSACheckersEmitter.cpp @@ -23,7 +23,7 @@ using namespace llvm; // Static Analyzer Checkers Tables generation //===----------------------------------------------------------------------===// -/// \brief True if it is specified hidden or a parent package is specified +/// True if it is specified hidden or a parent package is specified /// as hidden, otherwise false. static bool isHidden(const Record &R) { if (R.getValueAsBit("Hidden")) diff --git a/utils/TableGen/NeonEmitter.cpp b/utils/TableGen/NeonEmitter.cpp index 8117d2f4a232..eca03a5892e2 100644 --- a/utils/TableGen/NeonEmitter.cpp +++ b/utils/TableGen/NeonEmitter.cpp @@ -304,7 +304,7 @@ class Intrinsic { ListInit *Body; /// The architectural #ifdef guard. std::string Guard; - /// Set if the Unvailable bit is 1. This means we don't generate a body, + /// Set if the Unavailable bit is 1. This means we don't generate a body, /// just an "unavailable" attribute on a declaration. bool IsUnavailable; /// Is this intrinsic safe for big-endian? or does it need its arguments @@ -552,7 +552,11 @@ public: // run - Emit arm_neon.h.inc void run(raw_ostream &o); + // runFP16 - Emit arm_fp16.h.inc + void runFP16(raw_ostream &o); + // runHeader - Emit all the __builtin prototypes used in arm_neon.h + // and arm_fp16.h void runHeader(raw_ostream &o); // runTests - Emit tests for all the Neon intrinsics. @@ -852,6 +856,35 @@ void Type::applyModifier(char Mod) { NumVectors = 0; Float = true; break; + case 'Y': + Bitwidth = ElementBitwidth = 16; + NumVectors = 0; + Float = true; + break; + case 'I': + Bitwidth = ElementBitwidth = 32; + NumVectors = 0; + Float = false; + Signed = true; + break; + case 'L': + Bitwidth = ElementBitwidth = 64; + NumVectors = 0; + Float = false; + Signed = true; + break; + case 'U': + Bitwidth = ElementBitwidth = 32; + NumVectors = 0; + Float = false; + Signed = false; + break; + case 'O': + Bitwidth = ElementBitwidth = 64; + NumVectors = 0; + Float = false; + Signed = false; + break; case 'f': Float = true; ElementBitwidth = 32; @@ -915,7 +948,7 @@ void Type::applyModifier(char Mod) { break; case 'c': Constant = true; - // Fall through + LLVM_FALLTHROUGH; case 'p': Pointer = true; Bitwidth = ElementBitwidth; @@ -962,6 +995,19 @@ void Type::applyModifier(char Mod) { if (!AppliedQuad) Bitwidth *= 2; break; + case '7': + if (AppliedQuad) + Bitwidth /= 2; + ElementBitwidth = 8; + break; + case '8': + ElementBitwidth = 8; + break; + case '9': + if (!AppliedQuad) + Bitwidth *= 2; + ElementBitwidth = 8; + break; default: llvm_unreachable("Unhandled character!"); } @@ -1010,7 +1056,7 @@ std::string Intrinsic::getInstTypeCode(Type T, ClassKind CK) const { } static bool isFloatingPointProtoModifier(char Mod) { - return Mod == 'F' || Mod == 'f' || Mod == 'H'; + return Mod == 'F' || Mod == 'f' || Mod == 'H' || Mod == 'Y' || Mod == 'I'; } std::string Intrinsic::getBuiltinTypeStr() { @@ -1974,7 +2020,7 @@ void NeonEmitter::createIntrinsic(Record *R, } } - std::sort(NewTypeSpecs.begin(), NewTypeSpecs.end()); + llvm::sort(NewTypeSpecs.begin(), NewTypeSpecs.end()); NewTypeSpecs.erase(std::unique(NewTypeSpecs.begin(), NewTypeSpecs.end()), NewTypeSpecs.end()); auto &Entry = IntrinsicMap[Name]; @@ -2116,8 +2162,7 @@ void NeonEmitter::genOverloadTypeCheckCode(raw_ostream &OS, OS << "#endif\n\n"; } -void -NeonEmitter::genIntrinsicRangeCheckCode(raw_ostream &OS, +void NeonEmitter::genIntrinsicRangeCheckCode(raw_ostream &OS, SmallVectorImpl<Intrinsic *> &Defs) { OS << "#ifdef GET_NEON_IMMEDIATE_CHECK\n"; @@ -2142,11 +2187,15 @@ NeonEmitter::genIntrinsicRangeCheckCode(raw_ostream &OS, Record *R = Def->getRecord(); if (R->getValueAsBit("isVCVT_N")) { // VCVT between floating- and fixed-point values takes an immediate - // in the range [1, 32) for f32 or [1, 64) for f64. + // in the range [1, 32) for f32 or [1, 64) for f64 or [1, 16) for f16. LowerBound = "1"; - if (Def->getBaseType().getElementSizeInBits() == 32) + if (Def->getBaseType().getElementSizeInBits() == 16 || + Def->getName().find('h') != std::string::npos) + // VCVTh operating on FP16 intrinsics in range [1, 16) + UpperBound = "15"; + else if (Def->getBaseType().getElementSizeInBits() == 32) UpperBound = "31"; - else + else UpperBound = "63"; } else if (R->getValueAsBit("isScalarShift")) { // Right shifts have an 'r' in the name, left shifts do not. Convert @@ -2420,12 +2469,125 @@ void NeonEmitter::run(raw_ostream &OS) { OS << "#endif /* __ARM_NEON_H */\n"; } +/// run - Read the records in arm_fp16.td and output arm_fp16.h. arm_fp16.h +/// is comprised of type definitions and function declarations. +void NeonEmitter::runFP16(raw_ostream &OS) { + OS << "/*===---- arm_fp16.h - ARM FP16 intrinsics " + "------------------------------" + "---===\n" + " *\n" + " * Permission is hereby granted, free of charge, to any person " + "obtaining a copy\n" + " * of this software and associated documentation files (the " + "\"Software\"), to deal\n" + " * in the Software without restriction, including without limitation " + "the rights\n" + " * to use, copy, modify, merge, publish, distribute, sublicense, " + "and/or sell\n" + " * copies of the Software, and to permit persons to whom the Software " + "is\n" + " * furnished to do so, subject to the following conditions:\n" + " *\n" + " * The above copyright notice and this permission notice shall be " + "included in\n" + " * all copies or substantial portions of the Software.\n" + " *\n" + " * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, " + "EXPRESS OR\n" + " * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF " + "MERCHANTABILITY,\n" + " * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT " + "SHALL THE\n" + " * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR " + "OTHER\n" + " * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, " + "ARISING FROM,\n" + " * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER " + "DEALINGS IN\n" + " * THE SOFTWARE.\n" + " *\n" + " *===-----------------------------------------------------------------" + "---" + "---===\n" + " */\n\n"; + + OS << "#ifndef __ARM_FP16_H\n"; + OS << "#define __ARM_FP16_H\n\n"; + + OS << "#include <stdint.h>\n\n"; + + OS << "typedef __fp16 float16_t;\n"; + + OS << "#define __ai static inline __attribute__((__always_inline__, " + "__nodebug__))\n\n"; + + SmallVector<Intrinsic *, 128> Defs; + std::vector<Record *> RV = Records.getAllDerivedDefinitions("Inst"); + for (auto *R : RV) + createIntrinsic(R, Defs); + + for (auto *I : Defs) + I->indexBody(); + + std::stable_sort( + Defs.begin(), Defs.end(), + [](const Intrinsic *A, const Intrinsic *B) { return *A < *B; }); + + // Only emit a def when its requirements have been met. + // FIXME: This loop could be made faster, but it's fast enough for now. + bool MadeProgress = true; + std::string InGuard; + while (!Defs.empty() && MadeProgress) { + MadeProgress = false; + + for (SmallVector<Intrinsic *, 128>::iterator I = Defs.begin(); + I != Defs.end(); /*No step*/) { + bool DependenciesSatisfied = true; + for (auto *II : (*I)->getDependencies()) { + if (std::find(Defs.begin(), Defs.end(), II) != Defs.end()) + DependenciesSatisfied = false; + } + if (!DependenciesSatisfied) { + // Try the next one. + ++I; + continue; + } + + // Emit #endif/#if pair if needed. + if ((*I)->getGuard() != InGuard) { + if (!InGuard.empty()) + OS << "#endif\n"; + InGuard = (*I)->getGuard(); + if (!InGuard.empty()) + OS << "#if " << InGuard << "\n"; + } + + // Actually generate the intrinsic code. + OS << (*I)->generate(); + + MadeProgress = true; + I = Defs.erase(I); + } + } + assert(Defs.empty() && "Some requirements were not satisfied!"); + if (!InGuard.empty()) + OS << "#endif\n"; + + OS << "\n"; + OS << "#undef __ai\n\n"; + OS << "#endif /* __ARM_FP16_H */\n"; +} + namespace clang { void EmitNeon(RecordKeeper &Records, raw_ostream &OS) { NeonEmitter(Records).run(OS); } +void EmitFP16(RecordKeeper &Records, raw_ostream &OS) { + NeonEmitter(Records).runFP16(OS); +} + void EmitNeonSema(RecordKeeper &Records, raw_ostream &OS) { NeonEmitter(Records).runHeader(OS); } diff --git a/utils/TableGen/TableGen.cpp b/utils/TableGen/TableGen.cpp index 840b330a732c..a2ba131628f1 100644 --- a/utils/TableGen/TableGen.cpp +++ b/utils/TableGen/TableGen.cpp @@ -52,6 +52,7 @@ enum ActionType { GenClangCommentCommandInfo, GenClangCommentCommandList, GenArmNeon, + GenArmFP16, GenArmNeonSema, GenArmNeonTest, GenAttrDocs, @@ -139,6 +140,7 @@ cl::opt<ActionType> Action( "Generate list of commands that are used in " "documentation comments"), clEnumValN(GenArmNeon, "gen-arm-neon", "Generate arm_neon.h for clang"), + clEnumValN(GenArmFP16, "gen-arm-fp16", "Generate arm_fp16.h for clang"), clEnumValN(GenArmNeonSema, "gen-arm-neon-sema", "Generate ARM NEON sema support for clang"), clEnumValN(GenArmNeonTest, "gen-arm-neon-test", @@ -250,6 +252,9 @@ bool ClangTableGenMain(raw_ostream &OS, RecordKeeper &Records) { case GenArmNeon: EmitNeon(Records, OS); break; + case GenArmFP16: + EmitFP16(Records, OS); + break; case GenArmNeonSema: EmitNeonSema(Records, OS); break; diff --git a/utils/TableGen/TableGenBackends.h b/utils/TableGen/TableGenBackends.h index 342c889ca47a..706e812ae874 100644 --- a/utils/TableGen/TableGenBackends.h +++ b/utils/TableGen/TableGenBackends.h @@ -65,6 +65,7 @@ void EmitClangCommentCommandInfo(RecordKeeper &Records, raw_ostream &OS); void EmitClangCommentCommandList(RecordKeeper &Records, raw_ostream &OS); void EmitNeon(RecordKeeper &Records, raw_ostream &OS); +void EmitFP16(RecordKeeper &Records, raw_ostream &OS); void EmitNeonSema(RecordKeeper &Records, raw_ostream &OS); void EmitNeonTest(RecordKeeper &Records, raw_ostream &OS); void EmitNeon2(RecordKeeper &Records, raw_ostream &OS); |
