1 //===- ClangAttrEmitter.cpp - Generate Clang attribute handling =-*- C++ -*--=// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // These tablegen backends emit Clang attribute processing code 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/ADT/SmallString.h" 15 #include "llvm/ADT/STLExtras.h" 16 #include "llvm/ADT/SmallSet.h" 17 #include "llvm/ADT/StringSwitch.h" 18 #include "llvm/TableGen/Error.h" 19 #include "llvm/TableGen/Record.h" 20 #include "llvm/TableGen/StringMatcher.h" 21 #include "llvm/TableGen/TableGenBackend.h" 22 #include <algorithm> 23 #include <cctype> 24 #include <memory> 25 #include <set> 26 #include <sstream> 27 28 using namespace llvm; 29 30 class FlattenedSpelling { 31 std::string V, N, NS; 32 bool K; 33 34 public: 35 FlattenedSpelling(const std::string &Variety, const std::string &Name, 36 const std::string &Namespace, bool KnownToGCC) : 37 V(Variety), N(Name), NS(Namespace), K(KnownToGCC) {} 38 explicit FlattenedSpelling(const Record &Spelling) : 39 V(Spelling.getValueAsString("Variety")), 40 N(Spelling.getValueAsString("Name")) { 41 42 assert(V != "GCC" && "Given a GCC spelling, which means this hasn't been" 43 "flattened!"); 44 if (V == "CXX11") 45 NS = Spelling.getValueAsString("Namespace"); 46 bool Unset; 47 K = Spelling.getValueAsBitOrUnset("KnownToGCC", Unset); 48 } 49 50 const std::string &variety() const { return V; } 51 const std::string &name() const { return N; } 52 const std::string &nameSpace() const { return NS; } 53 bool knownToGCC() const { return K; } 54 }; 55 56 std::vector<FlattenedSpelling> GetFlattenedSpellings(const Record &Attr) { 57 std::vector<Record *> Spellings = Attr.getValueAsListOfDefs("Spellings"); 58 std::vector<FlattenedSpelling> Ret; 59 60 for (const auto &Spelling : Spellings) { 61 if (Spelling->getValueAsString("Variety") == "GCC") { 62 // Gin up two new spelling objects to add into the list. 63 Ret.push_back(FlattenedSpelling("GNU", Spelling->getValueAsString("Name"), 64 "", true)); 65 Ret.push_back(FlattenedSpelling( 66 "CXX11", Spelling->getValueAsString("Name"), "gnu", true)); 67 } else 68 Ret.push_back(FlattenedSpelling(*Spelling)); 69 } 70 71 return Ret; 72 } 73 74 static std::string ReadPCHRecord(StringRef type) { 75 return StringSwitch<std::string>(type) 76 .EndsWith("Decl *", "GetLocalDeclAs<" 77 + std::string(type, 0, type.size()-1) + ">(F, Record[Idx++])") 78 .Case("TypeSourceInfo *", "GetTypeSourceInfo(F, Record, Idx)") 79 .Case("Expr *", "ReadExpr(F)") 80 .Case("IdentifierInfo *", "GetIdentifierInfo(F, Record, Idx)") 81 .Default("Record[Idx++]"); 82 } 83 84 // Assumes that the way to get the value is SA->getname() 85 static std::string WritePCHRecord(StringRef type, StringRef name) { 86 return StringSwitch<std::string>(type) 87 .EndsWith("Decl *", "AddDeclRef(" + std::string(name) + 88 ", Record);\n") 89 .Case("TypeSourceInfo *", 90 "AddTypeSourceInfo(" + std::string(name) + ", Record);\n") 91 .Case("Expr *", "AddStmt(" + std::string(name) + ");\n") 92 .Case("IdentifierInfo *", 93 "AddIdentifierRef(" + std::string(name) + ", Record);\n") 94 .Default("Record.push_back(" + std::string(name) + ");\n"); 95 } 96 97 // Normalize attribute name by removing leading and trailing 98 // underscores. For example, __foo, foo__, __foo__ would 99 // become foo. 100 static StringRef NormalizeAttrName(StringRef AttrName) { 101 if (AttrName.startswith("__")) 102 AttrName = AttrName.substr(2, AttrName.size()); 103 104 if (AttrName.endswith("__")) 105 AttrName = AttrName.substr(0, AttrName.size() - 2); 106 107 return AttrName; 108 } 109 110 // Normalize the name by removing any and all leading and trailing underscores. 111 // This is different from NormalizeAttrName in that it also handles names like 112 // _pascal and __pascal. 113 static StringRef NormalizeNameForSpellingComparison(StringRef Name) { 114 while (Name.startswith("_")) 115 Name = Name.substr(1, Name.size()); 116 while (Name.endswith("_")) 117 Name = Name.substr(0, Name.size() - 1); 118 return Name; 119 } 120 121 // Normalize attribute spelling only if the spelling has both leading 122 // and trailing underscores. For example, __ms_struct__ will be 123 // normalized to "ms_struct"; __cdecl will remain intact. 124 static StringRef NormalizeAttrSpelling(StringRef AttrSpelling) { 125 if (AttrSpelling.startswith("__") && AttrSpelling.endswith("__")) { 126 AttrSpelling = AttrSpelling.substr(2, AttrSpelling.size() - 4); 127 } 128 129 return AttrSpelling; 130 } 131 132 typedef std::vector<std::pair<std::string, const Record *>> ParsedAttrMap; 133 134 static ParsedAttrMap getParsedAttrList(const RecordKeeper &Records, 135 ParsedAttrMap *Dupes = nullptr) { 136 std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr"); 137 std::set<std::string> Seen; 138 ParsedAttrMap R; 139 for (const auto *Attr : Attrs) { 140 if (Attr->getValueAsBit("SemaHandler")) { 141 std::string AN; 142 if (Attr->isSubClassOf("TargetSpecificAttr") && 143 !Attr->isValueUnset("ParseKind")) { 144 AN = Attr->getValueAsString("ParseKind"); 145 146 // If this attribute has already been handled, it does not need to be 147 // handled again. 148 if (Seen.find(AN) != Seen.end()) { 149 if (Dupes) 150 Dupes->push_back(std::make_pair(AN, Attr)); 151 continue; 152 } 153 Seen.insert(AN); 154 } else 155 AN = NormalizeAttrName(Attr->getName()).str(); 156 157 R.push_back(std::make_pair(AN, Attr)); 158 } 159 } 160 return R; 161 } 162 163 namespace { 164 class Argument { 165 std::string lowerName, upperName; 166 StringRef attrName; 167 bool isOpt; 168 169 public: 170 Argument(const Record &Arg, StringRef Attr) 171 : lowerName(Arg.getValueAsString("Name")), upperName(lowerName), 172 attrName(Attr), isOpt(false) { 173 if (!lowerName.empty()) { 174 lowerName[0] = std::tolower(lowerName[0]); 175 upperName[0] = std::toupper(upperName[0]); 176 } 177 } 178 virtual ~Argument() {} 179 180 StringRef getLowerName() const { return lowerName; } 181 StringRef getUpperName() const { return upperName; } 182 StringRef getAttrName() const { return attrName; } 183 184 bool isOptional() const { return isOpt; } 185 void setOptional(bool set) { isOpt = set; } 186 187 // These functions print the argument contents formatted in different ways. 188 virtual void writeAccessors(raw_ostream &OS) const = 0; 189 virtual void writeAccessorDefinitions(raw_ostream &OS) const {} 190 virtual void writeASTVisitorTraversal(raw_ostream &OS) const {} 191 virtual void writeCloneArgs(raw_ostream &OS) const = 0; 192 virtual void writeTemplateInstantiationArgs(raw_ostream &OS) const = 0; 193 virtual void writeTemplateInstantiation(raw_ostream &OS) const {} 194 virtual void writeCtorBody(raw_ostream &OS) const {} 195 virtual void writeCtorInitializers(raw_ostream &OS) const = 0; 196 virtual void writeCtorDefaultInitializers(raw_ostream &OS) const = 0; 197 virtual void writeCtorParameters(raw_ostream &OS) const = 0; 198 virtual void writeDeclarations(raw_ostream &OS) const = 0; 199 virtual void writePCHReadArgs(raw_ostream &OS) const = 0; 200 virtual void writePCHReadDecls(raw_ostream &OS) const = 0; 201 virtual void writePCHWrite(raw_ostream &OS) const = 0; 202 virtual void writeValue(raw_ostream &OS) const = 0; 203 virtual void writeDump(raw_ostream &OS) const = 0; 204 virtual void writeDumpChildren(raw_ostream &OS) const {} 205 virtual void writeHasChildren(raw_ostream &OS) const { OS << "false"; } 206 207 virtual bool isEnumArg() const { return false; } 208 virtual bool isVariadicEnumArg() const { return false; } 209 210 virtual void writeImplicitCtorArgs(raw_ostream &OS) const { 211 OS << getUpperName(); 212 } 213 }; 214 215 class SimpleArgument : public Argument { 216 std::string type; 217 218 public: 219 SimpleArgument(const Record &Arg, StringRef Attr, std::string T) 220 : Argument(Arg, Attr), type(T) 221 {} 222 223 std::string getType() const { return type; } 224 225 void writeAccessors(raw_ostream &OS) const override { 226 OS << " " << type << " get" << getUpperName() << "() const {\n"; 227 OS << " return " << getLowerName() << ";\n"; 228 OS << " }"; 229 } 230 void writeCloneArgs(raw_ostream &OS) const override { 231 OS << getLowerName(); 232 } 233 void writeTemplateInstantiationArgs(raw_ostream &OS) const override { 234 OS << "A->get" << getUpperName() << "()"; 235 } 236 void writeCtorInitializers(raw_ostream &OS) const override { 237 OS << getLowerName() << "(" << getUpperName() << ")"; 238 } 239 void writeCtorDefaultInitializers(raw_ostream &OS) const override { 240 OS << getLowerName() << "()"; 241 } 242 void writeCtorParameters(raw_ostream &OS) const override { 243 OS << type << " " << getUpperName(); 244 } 245 void writeDeclarations(raw_ostream &OS) const override { 246 OS << type << " " << getLowerName() << ";"; 247 } 248 void writePCHReadDecls(raw_ostream &OS) const override { 249 std::string read = ReadPCHRecord(type); 250 OS << " " << type << " " << getLowerName() << " = " << read << ";\n"; 251 } 252 void writePCHReadArgs(raw_ostream &OS) const override { 253 OS << getLowerName(); 254 } 255 void writePCHWrite(raw_ostream &OS) const override { 256 OS << " " << WritePCHRecord(type, "SA->get" + 257 std::string(getUpperName()) + "()"); 258 } 259 void writeValue(raw_ostream &OS) const override { 260 if (type == "FunctionDecl *") { 261 OS << "\" << get" << getUpperName() 262 << "()->getNameInfo().getAsString() << \""; 263 } else if (type == "IdentifierInfo *") { 264 OS << "\" << get" << getUpperName() << "()->getName() << \""; 265 } else if (type == "TypeSourceInfo *") { 266 OS << "\" << get" << getUpperName() << "().getAsString() << \""; 267 } else { 268 OS << "\" << get" << getUpperName() << "() << \""; 269 } 270 } 271 void writeDump(raw_ostream &OS) const override { 272 if (type == "FunctionDecl *") { 273 OS << " OS << \" \";\n"; 274 OS << " dumpBareDeclRef(SA->get" << getUpperName() << "());\n"; 275 } else if (type == "IdentifierInfo *") { 276 OS << " OS << \" \" << SA->get" << getUpperName() 277 << "()->getName();\n"; 278 } else if (type == "TypeSourceInfo *") { 279 OS << " OS << \" \" << SA->get" << getUpperName() 280 << "().getAsString();\n"; 281 } else if (type == "bool") { 282 OS << " if (SA->get" << getUpperName() << "()) OS << \" " 283 << getUpperName() << "\";\n"; 284 } else if (type == "int" || type == "unsigned") { 285 OS << " OS << \" \" << SA->get" << getUpperName() << "();\n"; 286 } else { 287 llvm_unreachable("Unknown SimpleArgument type!"); 288 } 289 } 290 }; 291 292 class DefaultSimpleArgument : public SimpleArgument { 293 int64_t Default; 294 295 public: 296 DefaultSimpleArgument(const Record &Arg, StringRef Attr, 297 std::string T, int64_t Default) 298 : SimpleArgument(Arg, Attr, T), Default(Default) {} 299 300 void writeAccessors(raw_ostream &OS) const override { 301 SimpleArgument::writeAccessors(OS); 302 303 OS << "\n\n static const " << getType() << " Default" << getUpperName() 304 << " = " << Default << ";"; 305 } 306 }; 307 308 class StringArgument : public Argument { 309 public: 310 StringArgument(const Record &Arg, StringRef Attr) 311 : Argument(Arg, Attr) 312 {} 313 314 void writeAccessors(raw_ostream &OS) const override { 315 OS << " llvm::StringRef get" << getUpperName() << "() const {\n"; 316 OS << " return llvm::StringRef(" << getLowerName() << ", " 317 << getLowerName() << "Length);\n"; 318 OS << " }\n"; 319 OS << " unsigned get" << getUpperName() << "Length() const {\n"; 320 OS << " return " << getLowerName() << "Length;\n"; 321 OS << " }\n"; 322 OS << " void set" << getUpperName() 323 << "(ASTContext &C, llvm::StringRef S) {\n"; 324 OS << " " << getLowerName() << "Length = S.size();\n"; 325 OS << " this->" << getLowerName() << " = new (C, 1) char [" 326 << getLowerName() << "Length];\n"; 327 OS << " std::memcpy(this->" << getLowerName() << ", S.data(), " 328 << getLowerName() << "Length);\n"; 329 OS << " }"; 330 } 331 void writeCloneArgs(raw_ostream &OS) const override { 332 OS << "get" << getUpperName() << "()"; 333 } 334 void writeTemplateInstantiationArgs(raw_ostream &OS) const override { 335 OS << "A->get" << getUpperName() << "()"; 336 } 337 void writeCtorBody(raw_ostream &OS) const override { 338 OS << " std::memcpy(" << getLowerName() << ", " << getUpperName() 339 << ".data(), " << getLowerName() << "Length);"; 340 } 341 void writeCtorInitializers(raw_ostream &OS) const override { 342 OS << getLowerName() << "Length(" << getUpperName() << ".size())," 343 << getLowerName() << "(new (Ctx, 1) char[" << getLowerName() 344 << "Length])"; 345 } 346 void writeCtorDefaultInitializers(raw_ostream &OS) const override { 347 OS << getLowerName() << "Length(0)," << getLowerName() << "(0)"; 348 } 349 void writeCtorParameters(raw_ostream &OS) const override { 350 OS << "llvm::StringRef " << getUpperName(); 351 } 352 void writeDeclarations(raw_ostream &OS) const override { 353 OS << "unsigned " << getLowerName() << "Length;\n"; 354 OS << "char *" << getLowerName() << ";"; 355 } 356 void writePCHReadDecls(raw_ostream &OS) const override { 357 OS << " std::string " << getLowerName() 358 << "= ReadString(Record, Idx);\n"; 359 } 360 void writePCHReadArgs(raw_ostream &OS) const override { 361 OS << getLowerName(); 362 } 363 void writePCHWrite(raw_ostream &OS) const override { 364 OS << " AddString(SA->get" << getUpperName() << "(), Record);\n"; 365 } 366 void writeValue(raw_ostream &OS) const override { 367 OS << "\\\"\" << get" << getUpperName() << "() << \"\\\""; 368 } 369 void writeDump(raw_ostream &OS) const override { 370 OS << " OS << \" \\\"\" << SA->get" << getUpperName() 371 << "() << \"\\\"\";\n"; 372 } 373 }; 374 375 class AlignedArgument : public Argument { 376 public: 377 AlignedArgument(const Record &Arg, StringRef Attr) 378 : Argument(Arg, Attr) 379 {} 380 381 void writeAccessors(raw_ostream &OS) const override { 382 OS << " bool is" << getUpperName() << "Dependent() const;\n"; 383 384 OS << " unsigned get" << getUpperName() << "(ASTContext &Ctx) const;\n"; 385 386 OS << " bool is" << getUpperName() << "Expr() const {\n"; 387 OS << " return is" << getLowerName() << "Expr;\n"; 388 OS << " }\n"; 389 390 OS << " Expr *get" << getUpperName() << "Expr() const {\n"; 391 OS << " assert(is" << getLowerName() << "Expr);\n"; 392 OS << " return " << getLowerName() << "Expr;\n"; 393 OS << " }\n"; 394 395 OS << " TypeSourceInfo *get" << getUpperName() << "Type() const {\n"; 396 OS << " assert(!is" << getLowerName() << "Expr);\n"; 397 OS << " return " << getLowerName() << "Type;\n"; 398 OS << " }"; 399 } 400 void writeAccessorDefinitions(raw_ostream &OS) const override { 401 OS << "bool " << getAttrName() << "Attr::is" << getUpperName() 402 << "Dependent() const {\n"; 403 OS << " if (is" << getLowerName() << "Expr)\n"; 404 OS << " return " << getLowerName() << "Expr && (" << getLowerName() 405 << "Expr->isValueDependent() || " << getLowerName() 406 << "Expr->isTypeDependent());\n"; 407 OS << " else\n"; 408 OS << " return " << getLowerName() 409 << "Type->getType()->isDependentType();\n"; 410 OS << "}\n"; 411 412 // FIXME: Do not do the calculation here 413 // FIXME: Handle types correctly 414 // A null pointer means maximum alignment 415 // FIXME: Load the platform-specific maximum alignment, rather than 416 // 16, the x86 max. 417 OS << "unsigned " << getAttrName() << "Attr::get" << getUpperName() 418 << "(ASTContext &Ctx) const {\n"; 419 OS << " assert(!is" << getUpperName() << "Dependent());\n"; 420 OS << " if (is" << getLowerName() << "Expr)\n"; 421 OS << " return (" << getLowerName() << "Expr ? " << getLowerName() 422 << "Expr->EvaluateKnownConstInt(Ctx).getZExtValue() : 16)" 423 << "* Ctx.getCharWidth();\n"; 424 OS << " else\n"; 425 OS << " return 0; // FIXME\n"; 426 OS << "}\n"; 427 } 428 void writeCloneArgs(raw_ostream &OS) const override { 429 OS << "is" << getLowerName() << "Expr, is" << getLowerName() 430 << "Expr ? static_cast<void*>(" << getLowerName() 431 << "Expr) : " << getLowerName() 432 << "Type"; 433 } 434 void writeTemplateInstantiationArgs(raw_ostream &OS) const override { 435 // FIXME: move the definition in Sema::InstantiateAttrs to here. 436 // In the meantime, aligned attributes are cloned. 437 } 438 void writeCtorBody(raw_ostream &OS) const override { 439 OS << " if (is" << getLowerName() << "Expr)\n"; 440 OS << " " << getLowerName() << "Expr = reinterpret_cast<Expr *>(" 441 << getUpperName() << ");\n"; 442 OS << " else\n"; 443 OS << " " << getLowerName() 444 << "Type = reinterpret_cast<TypeSourceInfo *>(" << getUpperName() 445 << ");"; 446 } 447 void writeCtorInitializers(raw_ostream &OS) const override { 448 OS << "is" << getLowerName() << "Expr(Is" << getUpperName() << "Expr)"; 449 } 450 void writeCtorDefaultInitializers(raw_ostream &OS) const override { 451 OS << "is" << getLowerName() << "Expr(false)"; 452 } 453 void writeCtorParameters(raw_ostream &OS) const override { 454 OS << "bool Is" << getUpperName() << "Expr, void *" << getUpperName(); 455 } 456 void writeImplicitCtorArgs(raw_ostream &OS) const override { 457 OS << "Is" << getUpperName() << "Expr, " << getUpperName(); 458 } 459 void writeDeclarations(raw_ostream &OS) const override { 460 OS << "bool is" << getLowerName() << "Expr;\n"; 461 OS << "union {\n"; 462 OS << "Expr *" << getLowerName() << "Expr;\n"; 463 OS << "TypeSourceInfo *" << getLowerName() << "Type;\n"; 464 OS << "};"; 465 } 466 void writePCHReadArgs(raw_ostream &OS) const override { 467 OS << "is" << getLowerName() << "Expr, " << getLowerName() << "Ptr"; 468 } 469 void writePCHReadDecls(raw_ostream &OS) const override { 470 OS << " bool is" << getLowerName() << "Expr = Record[Idx++];\n"; 471 OS << " void *" << getLowerName() << "Ptr;\n"; 472 OS << " if (is" << getLowerName() << "Expr)\n"; 473 OS << " " << getLowerName() << "Ptr = ReadExpr(F);\n"; 474 OS << " else\n"; 475 OS << " " << getLowerName() 476 << "Ptr = GetTypeSourceInfo(F, Record, Idx);\n"; 477 } 478 void writePCHWrite(raw_ostream &OS) const override { 479 OS << " Record.push_back(SA->is" << getUpperName() << "Expr());\n"; 480 OS << " if (SA->is" << getUpperName() << "Expr())\n"; 481 OS << " AddStmt(SA->get" << getUpperName() << "Expr());\n"; 482 OS << " else\n"; 483 OS << " AddTypeSourceInfo(SA->get" << getUpperName() 484 << "Type(), Record);\n"; 485 } 486 void writeValue(raw_ostream &OS) const override { 487 OS << "\";\n" 488 << " " << getLowerName() << "Expr->printPretty(OS, 0, Policy);\n" 489 << " OS << \""; 490 } 491 void writeDump(raw_ostream &OS) const override { 492 } 493 void writeDumpChildren(raw_ostream &OS) const override { 494 OS << " if (SA->is" << getUpperName() << "Expr()) {\n"; 495 OS << " lastChild();\n"; 496 OS << " dumpStmt(SA->get" << getUpperName() << "Expr());\n"; 497 OS << " } else\n"; 498 OS << " dumpType(SA->get" << getUpperName() 499 << "Type()->getType());\n"; 500 } 501 void writeHasChildren(raw_ostream &OS) const override { 502 OS << "SA->is" << getUpperName() << "Expr()"; 503 } 504 }; 505 506 class VariadicArgument : public Argument { 507 std::string Type, ArgName, ArgSizeName, RangeName; 508 509 public: 510 VariadicArgument(const Record &Arg, StringRef Attr, std::string T) 511 : Argument(Arg, Attr), Type(T), ArgName(getLowerName().str() + "_"), 512 ArgSizeName(ArgName + "Size"), RangeName(getLowerName()) {} 513 514 std::string getType() const { return Type; } 515 516 void writeAccessors(raw_ostream &OS) const override { 517 std::string IteratorType = getLowerName().str() + "_iterator"; 518 std::string BeginFn = getLowerName().str() + "_begin()"; 519 std::string EndFn = getLowerName().str() + "_end()"; 520 521 OS << " typedef " << Type << "* " << IteratorType << ";\n"; 522 OS << " " << IteratorType << " " << BeginFn << " const {" 523 << " return " << ArgName << "; }\n"; 524 OS << " " << IteratorType << " " << EndFn << " const {" 525 << " return " << ArgName << " + " << ArgSizeName << "; }\n"; 526 OS << " unsigned " << getLowerName() << "_size() const {" 527 << " return " << ArgSizeName << "; }\n"; 528 OS << " llvm::iterator_range<" << IteratorType << "> " << RangeName 529 << "() const { return llvm::make_range(" << BeginFn << ", " << EndFn 530 << "); }\n"; 531 } 532 void writeCloneArgs(raw_ostream &OS) const override { 533 OS << ArgName << ", " << ArgSizeName; 534 } 535 void writeTemplateInstantiationArgs(raw_ostream &OS) const override { 536 // This isn't elegant, but we have to go through public methods... 537 OS << "A->" << getLowerName() << "_begin(), " 538 << "A->" << getLowerName() << "_size()"; 539 } 540 void writeCtorBody(raw_ostream &OS) const override { 541 OS << " std::copy(" << getUpperName() << ", " << getUpperName() 542 << " + " << ArgSizeName << ", " << ArgName << ");"; 543 } 544 void writeCtorInitializers(raw_ostream &OS) const override { 545 OS << ArgSizeName << "(" << getUpperName() << "Size), " 546 << ArgName << "(new (Ctx, 16) " << getType() << "[" 547 << ArgSizeName << "])"; 548 } 549 void writeCtorDefaultInitializers(raw_ostream &OS) const override { 550 OS << ArgSizeName << "(0), " << ArgName << "(nullptr)"; 551 } 552 void writeCtorParameters(raw_ostream &OS) const override { 553 OS << getType() << " *" << getUpperName() << ", unsigned " 554 << getUpperName() << "Size"; 555 } 556 void writeImplicitCtorArgs(raw_ostream &OS) const override { 557 OS << getUpperName() << ", " << getUpperName() << "Size"; 558 } 559 void writeDeclarations(raw_ostream &OS) const override { 560 OS << " unsigned " << ArgSizeName << ";\n"; 561 OS << " " << getType() << " *" << ArgName << ";"; 562 } 563 void writePCHReadDecls(raw_ostream &OS) const override { 564 OS << " unsigned " << getLowerName() << "Size = Record[Idx++];\n"; 565 OS << " SmallVector<" << Type << ", 4> " << getLowerName() 566 << ";\n"; 567 OS << " " << getLowerName() << ".reserve(" << getLowerName() 568 << "Size);\n"; 569 OS << " for (unsigned i = " << getLowerName() << "Size; i; --i)\n"; 570 571 std::string read = ReadPCHRecord(Type); 572 OS << " " << getLowerName() << ".push_back(" << read << ");\n"; 573 } 574 void writePCHReadArgs(raw_ostream &OS) const override { 575 OS << getLowerName() << ".data(), " << getLowerName() << "Size"; 576 } 577 void writePCHWrite(raw_ostream &OS) const override { 578 OS << " Record.push_back(SA->" << getLowerName() << "_size());\n"; 579 OS << " for (auto &Val : SA->" << RangeName << "())\n"; 580 OS << " " << WritePCHRecord(Type, "Val"); 581 } 582 void writeValue(raw_ostream &OS) const override { 583 OS << "\";\n"; 584 OS << " bool isFirst = true;\n" 585 << " for (const auto &Val : " << RangeName << "()) {\n" 586 << " if (isFirst) isFirst = false;\n" 587 << " else OS << \", \";\n" 588 << " OS << Val;\n" 589 << " }\n"; 590 OS << " OS << \""; 591 } 592 void writeDump(raw_ostream &OS) const override { 593 OS << " for (const auto &Val : SA->" << RangeName << "())\n"; 594 OS << " OS << \" \" << Val;\n"; 595 } 596 }; 597 598 // Unique the enums, but maintain the original declaration ordering. 599 std::vector<std::string> 600 uniqueEnumsInOrder(const std::vector<std::string> &enums) { 601 std::vector<std::string> uniques; 602 std::set<std::string> unique_set(enums.begin(), enums.end()); 603 for (const auto &i : enums) { 604 std::set<std::string>::iterator set_i = unique_set.find(i); 605 if (set_i != unique_set.end()) { 606 uniques.push_back(i); 607 unique_set.erase(set_i); 608 } 609 } 610 return uniques; 611 } 612 613 class EnumArgument : public Argument { 614 std::string type; 615 std::vector<std::string> values, enums, uniques; 616 public: 617 EnumArgument(const Record &Arg, StringRef Attr) 618 : Argument(Arg, Attr), type(Arg.getValueAsString("Type")), 619 values(Arg.getValueAsListOfStrings("Values")), 620 enums(Arg.getValueAsListOfStrings("Enums")), 621 uniques(uniqueEnumsInOrder(enums)) 622 { 623 // FIXME: Emit a proper error 624 assert(!uniques.empty()); 625 } 626 627 bool isEnumArg() const override { return true; } 628 629 void writeAccessors(raw_ostream &OS) const override { 630 OS << " " << type << " get" << getUpperName() << "() const {\n"; 631 OS << " return " << getLowerName() << ";\n"; 632 OS << " }"; 633 } 634 void writeCloneArgs(raw_ostream &OS) const override { 635 OS << getLowerName(); 636 } 637 void writeTemplateInstantiationArgs(raw_ostream &OS) const override { 638 OS << "A->get" << getUpperName() << "()"; 639 } 640 void writeCtorInitializers(raw_ostream &OS) const override { 641 OS << getLowerName() << "(" << getUpperName() << ")"; 642 } 643 void writeCtorDefaultInitializers(raw_ostream &OS) const override { 644 OS << getLowerName() << "(" << type << "(0))"; 645 } 646 void writeCtorParameters(raw_ostream &OS) const override { 647 OS << type << " " << getUpperName(); 648 } 649 void writeDeclarations(raw_ostream &OS) const override { 650 std::vector<std::string>::const_iterator i = uniques.begin(), 651 e = uniques.end(); 652 // The last one needs to not have a comma. 653 --e; 654 655 OS << "public:\n"; 656 OS << " enum " << type << " {\n"; 657 for (; i != e; ++i) 658 OS << " " << *i << ",\n"; 659 OS << " " << *e << "\n"; 660 OS << " };\n"; 661 OS << "private:\n"; 662 OS << " " << type << " " << getLowerName() << ";"; 663 } 664 void writePCHReadDecls(raw_ostream &OS) const override { 665 OS << " " << getAttrName() << "Attr::" << type << " " << getLowerName() 666 << "(static_cast<" << getAttrName() << "Attr::" << type 667 << ">(Record[Idx++]));\n"; 668 } 669 void writePCHReadArgs(raw_ostream &OS) const override { 670 OS << getLowerName(); 671 } 672 void writePCHWrite(raw_ostream &OS) const override { 673 OS << "Record.push_back(SA->get" << getUpperName() << "());\n"; 674 } 675 void writeValue(raw_ostream &OS) const override { 676 OS << "\" << get" << getUpperName() << "() << \""; 677 } 678 void writeDump(raw_ostream &OS) const override { 679 OS << " switch(SA->get" << getUpperName() << "()) {\n"; 680 for (const auto &I : uniques) { 681 OS << " case " << getAttrName() << "Attr::" << I << ":\n"; 682 OS << " OS << \" " << I << "\";\n"; 683 OS << " break;\n"; 684 } 685 OS << " }\n"; 686 } 687 688 void writeConversion(raw_ostream &OS) const { 689 OS << " static bool ConvertStrTo" << type << "(StringRef Val, "; 690 OS << type << " &Out) {\n"; 691 OS << " Optional<" << type << "> R = llvm::StringSwitch<Optional<"; 692 OS << type << ">>(Val)\n"; 693 for (size_t I = 0; I < enums.size(); ++I) { 694 OS << " .Case(\"" << values[I] << "\", "; 695 OS << getAttrName() << "Attr::" << enums[I] << ")\n"; 696 } 697 OS << " .Default(Optional<" << type << ">());\n"; 698 OS << " if (R) {\n"; 699 OS << " Out = *R;\n return true;\n }\n"; 700 OS << " return false;\n"; 701 OS << " }\n"; 702 } 703 }; 704 705 class VariadicEnumArgument: public VariadicArgument { 706 std::string type, QualifiedTypeName; 707 std::vector<std::string> values, enums, uniques; 708 public: 709 VariadicEnumArgument(const Record &Arg, StringRef Attr) 710 : VariadicArgument(Arg, Attr, Arg.getValueAsString("Type")), 711 type(Arg.getValueAsString("Type")), 712 values(Arg.getValueAsListOfStrings("Values")), 713 enums(Arg.getValueAsListOfStrings("Enums")), 714 uniques(uniqueEnumsInOrder(enums)) 715 { 716 QualifiedTypeName = getAttrName().str() + "Attr::" + type; 717 718 // FIXME: Emit a proper error 719 assert(!uniques.empty()); 720 } 721 722 bool isVariadicEnumArg() const override { return true; } 723 724 void writeDeclarations(raw_ostream &OS) const override { 725 std::vector<std::string>::const_iterator i = uniques.begin(), 726 e = uniques.end(); 727 // The last one needs to not have a comma. 728 --e; 729 730 OS << "public:\n"; 731 OS << " enum " << type << " {\n"; 732 for (; i != e; ++i) 733 OS << " " << *i << ",\n"; 734 OS << " " << *e << "\n"; 735 OS << " };\n"; 736 OS << "private:\n"; 737 738 VariadicArgument::writeDeclarations(OS); 739 } 740 void writeDump(raw_ostream &OS) const override { 741 OS << " for (" << getAttrName() << "Attr::" << getLowerName() 742 << "_iterator I = SA->" << getLowerName() << "_begin(), E = SA->" 743 << getLowerName() << "_end(); I != E; ++I) {\n"; 744 OS << " switch(*I) {\n"; 745 for (const auto &UI : uniques) { 746 OS << " case " << getAttrName() << "Attr::" << UI << ":\n"; 747 OS << " OS << \" " << UI << "\";\n"; 748 OS << " break;\n"; 749 } 750 OS << " }\n"; 751 OS << " }\n"; 752 } 753 void writePCHReadDecls(raw_ostream &OS) const override { 754 OS << " unsigned " << getLowerName() << "Size = Record[Idx++];\n"; 755 OS << " SmallVector<" << QualifiedTypeName << ", 4> " << getLowerName() 756 << ";\n"; 757 OS << " " << getLowerName() << ".reserve(" << getLowerName() 758 << "Size);\n"; 759 OS << " for (unsigned i = " << getLowerName() << "Size; i; --i)\n"; 760 OS << " " << getLowerName() << ".push_back(" << "static_cast<" 761 << QualifiedTypeName << ">(Record[Idx++]));\n"; 762 } 763 void writePCHWrite(raw_ostream &OS) const override { 764 OS << " Record.push_back(SA->" << getLowerName() << "_size());\n"; 765 OS << " for (" << getAttrName() << "Attr::" << getLowerName() 766 << "_iterator i = SA->" << getLowerName() << "_begin(), e = SA->" 767 << getLowerName() << "_end(); i != e; ++i)\n"; 768 OS << " " << WritePCHRecord(QualifiedTypeName, "(*i)"); 769 } 770 void writeConversion(raw_ostream &OS) const { 771 OS << " static bool ConvertStrTo" << type << "(StringRef Val, "; 772 OS << type << " &Out) {\n"; 773 OS << " Optional<" << type << "> R = llvm::StringSwitch<Optional<"; 774 OS << type << ">>(Val)\n"; 775 for (size_t I = 0; I < enums.size(); ++I) { 776 OS << " .Case(\"" << values[I] << "\", "; 777 OS << getAttrName() << "Attr::" << enums[I] << ")\n"; 778 } 779 OS << " .Default(Optional<" << type << ">());\n"; 780 OS << " if (R) {\n"; 781 OS << " Out = *R;\n return true;\n }\n"; 782 OS << " return false;\n"; 783 OS << " }\n"; 784 } 785 }; 786 787 class VersionArgument : public Argument { 788 public: 789 VersionArgument(const Record &Arg, StringRef Attr) 790 : Argument(Arg, Attr) 791 {} 792 793 void writeAccessors(raw_ostream &OS) const override { 794 OS << " VersionTuple get" << getUpperName() << "() const {\n"; 795 OS << " return " << getLowerName() << ";\n"; 796 OS << " }\n"; 797 OS << " void set" << getUpperName() 798 << "(ASTContext &C, VersionTuple V) {\n"; 799 OS << " " << getLowerName() << " = V;\n"; 800 OS << " }"; 801 } 802 void writeCloneArgs(raw_ostream &OS) const override { 803 OS << "get" << getUpperName() << "()"; 804 } 805 void writeTemplateInstantiationArgs(raw_ostream &OS) const override { 806 OS << "A->get" << getUpperName() << "()"; 807 } 808 void writeCtorInitializers(raw_ostream &OS) const override { 809 OS << getLowerName() << "(" << getUpperName() << ")"; 810 } 811 void writeCtorDefaultInitializers(raw_ostream &OS) const override { 812 OS << getLowerName() << "()"; 813 } 814 void writeCtorParameters(raw_ostream &OS) const override { 815 OS << "VersionTuple " << getUpperName(); 816 } 817 void writeDeclarations(raw_ostream &OS) const override { 818 OS << "VersionTuple " << getLowerName() << ";\n"; 819 } 820 void writePCHReadDecls(raw_ostream &OS) const override { 821 OS << " VersionTuple " << getLowerName() 822 << "= ReadVersionTuple(Record, Idx);\n"; 823 } 824 void writePCHReadArgs(raw_ostream &OS) const override { 825 OS << getLowerName(); 826 } 827 void writePCHWrite(raw_ostream &OS) const override { 828 OS << " AddVersionTuple(SA->get" << getUpperName() << "(), Record);\n"; 829 } 830 void writeValue(raw_ostream &OS) const override { 831 OS << getLowerName() << "=\" << get" << getUpperName() << "() << \""; 832 } 833 void writeDump(raw_ostream &OS) const override { 834 OS << " OS << \" \" << SA->get" << getUpperName() << "();\n"; 835 } 836 }; 837 838 class ExprArgument : public SimpleArgument { 839 public: 840 ExprArgument(const Record &Arg, StringRef Attr) 841 : SimpleArgument(Arg, Attr, "Expr *") 842 {} 843 844 void writeASTVisitorTraversal(raw_ostream &OS) const override { 845 OS << " if (!" 846 << "getDerived().TraverseStmt(A->get" << getUpperName() << "()))\n"; 847 OS << " return false;\n"; 848 } 849 850 void writeTemplateInstantiationArgs(raw_ostream &OS) const override { 851 OS << "tempInst" << getUpperName(); 852 } 853 854 void writeTemplateInstantiation(raw_ostream &OS) const override { 855 OS << " " << getType() << " tempInst" << getUpperName() << ";\n"; 856 OS << " {\n"; 857 OS << " EnterExpressionEvaluationContext " 858 << "Unevaluated(S, Sema::Unevaluated);\n"; 859 OS << " ExprResult " << "Result = S.SubstExpr(" 860 << "A->get" << getUpperName() << "(), TemplateArgs);\n"; 861 OS << " tempInst" << getUpperName() << " = " 862 << "Result.getAs<Expr>();\n"; 863 OS << " }\n"; 864 } 865 866 void writeDump(raw_ostream &OS) const override {} 867 868 void writeDumpChildren(raw_ostream &OS) const override { 869 OS << " lastChild();\n"; 870 OS << " dumpStmt(SA->get" << getUpperName() << "());\n"; 871 } 872 void writeHasChildren(raw_ostream &OS) const override { OS << "true"; } 873 }; 874 875 class VariadicExprArgument : public VariadicArgument { 876 public: 877 VariadicExprArgument(const Record &Arg, StringRef Attr) 878 : VariadicArgument(Arg, Attr, "Expr *") 879 {} 880 881 void writeASTVisitorTraversal(raw_ostream &OS) const override { 882 OS << " {\n"; 883 OS << " " << getType() << " *I = A->" << getLowerName() 884 << "_begin();\n"; 885 OS << " " << getType() << " *E = A->" << getLowerName() 886 << "_end();\n"; 887 OS << " for (; I != E; ++I) {\n"; 888 OS << " if (!getDerived().TraverseStmt(*I))\n"; 889 OS << " return false;\n"; 890 OS << " }\n"; 891 OS << " }\n"; 892 } 893 894 void writeTemplateInstantiationArgs(raw_ostream &OS) const override { 895 OS << "tempInst" << getUpperName() << ", " 896 << "A->" << getLowerName() << "_size()"; 897 } 898 899 void writeTemplateInstantiation(raw_ostream &OS) const override { 900 OS << " " << getType() << " *tempInst" << getUpperName() 901 << " = new (C, 16) " << getType() 902 << "[A->" << getLowerName() << "_size()];\n"; 903 OS << " {\n"; 904 OS << " EnterExpressionEvaluationContext " 905 << "Unevaluated(S, Sema::Unevaluated);\n"; 906 OS << " " << getType() << " *TI = tempInst" << getUpperName() 907 << ";\n"; 908 OS << " " << getType() << " *I = A->" << getLowerName() 909 << "_begin();\n"; 910 OS << " " << getType() << " *E = A->" << getLowerName() 911 << "_end();\n"; 912 OS << " for (; I != E; ++I, ++TI) {\n"; 913 OS << " ExprResult Result = S.SubstExpr(*I, TemplateArgs);\n"; 914 OS << " *TI = Result.getAs<Expr>();\n"; 915 OS << " }\n"; 916 OS << " }\n"; 917 } 918 919 void writeDump(raw_ostream &OS) const override {} 920 921 void writeDumpChildren(raw_ostream &OS) const override { 922 OS << " for (" << getAttrName() << "Attr::" << getLowerName() 923 << "_iterator I = SA->" << getLowerName() << "_begin(), E = SA->" 924 << getLowerName() << "_end(); I != E; ++I) {\n"; 925 OS << " if (I + 1 == E)\n"; 926 OS << " lastChild();\n"; 927 OS << " dumpStmt(*I);\n"; 928 OS << " }\n"; 929 } 930 931 void writeHasChildren(raw_ostream &OS) const override { 932 OS << "SA->" << getLowerName() << "_begin() != " 933 << "SA->" << getLowerName() << "_end()"; 934 } 935 }; 936 937 class TypeArgument : public SimpleArgument { 938 public: 939 TypeArgument(const Record &Arg, StringRef Attr) 940 : SimpleArgument(Arg, Attr, "TypeSourceInfo *") 941 {} 942 943 void writeAccessors(raw_ostream &OS) const override { 944 OS << " QualType get" << getUpperName() << "() const {\n"; 945 OS << " return " << getLowerName() << "->getType();\n"; 946 OS << " }"; 947 OS << " " << getType() << " get" << getUpperName() << "Loc() const {\n"; 948 OS << " return " << getLowerName() << ";\n"; 949 OS << " }"; 950 } 951 void writeTemplateInstantiationArgs(raw_ostream &OS) const override { 952 OS << "A->get" << getUpperName() << "Loc()"; 953 } 954 void writePCHWrite(raw_ostream &OS) const override { 955 OS << " " << WritePCHRecord( 956 getType(), "SA->get" + std::string(getUpperName()) + "Loc()"); 957 } 958 }; 959 } 960 961 static std::unique_ptr<Argument> 962 createArgument(const Record &Arg, StringRef Attr, 963 const Record *Search = nullptr) { 964 if (!Search) 965 Search = &Arg; 966 967 Argument *Ptr = nullptr; 968 llvm::StringRef ArgName = Search->getName(); 969 970 if (ArgName == "AlignedArgument") Ptr = new AlignedArgument(Arg, Attr); 971 else if (ArgName == "EnumArgument") Ptr = new EnumArgument(Arg, Attr); 972 else if (ArgName == "ExprArgument") Ptr = new ExprArgument(Arg, Attr); 973 else if (ArgName == "FunctionArgument") 974 Ptr = new SimpleArgument(Arg, Attr, "FunctionDecl *"); 975 else if (ArgName == "IdentifierArgument") 976 Ptr = new SimpleArgument(Arg, Attr, "IdentifierInfo *"); 977 else if (ArgName == "DefaultBoolArgument") 978 Ptr = new DefaultSimpleArgument(Arg, Attr, "bool", 979 Arg.getValueAsBit("Default")); 980 else if (ArgName == "BoolArgument") Ptr = new SimpleArgument(Arg, Attr, 981 "bool"); 982 else if (ArgName == "DefaultIntArgument") 983 Ptr = new DefaultSimpleArgument(Arg, Attr, "int", 984 Arg.getValueAsInt("Default")); 985 else if (ArgName == "IntArgument") Ptr = new SimpleArgument(Arg, Attr, "int"); 986 else if (ArgName == "StringArgument") Ptr = new StringArgument(Arg, Attr); 987 else if (ArgName == "TypeArgument") Ptr = new TypeArgument(Arg, Attr); 988 else if (ArgName == "UnsignedArgument") 989 Ptr = new SimpleArgument(Arg, Attr, "unsigned"); 990 else if (ArgName == "VariadicUnsignedArgument") 991 Ptr = new VariadicArgument(Arg, Attr, "unsigned"); 992 else if (ArgName == "VariadicEnumArgument") 993 Ptr = new VariadicEnumArgument(Arg, Attr); 994 else if (ArgName == "VariadicExprArgument") 995 Ptr = new VariadicExprArgument(Arg, Attr); 996 else if (ArgName == "VersionArgument") 997 Ptr = new VersionArgument(Arg, Attr); 998 999 if (!Ptr) { 1000 // Search in reverse order so that the most-derived type is handled first. 1001 std::vector<Record*> Bases = Search->getSuperClasses(); 1002 for (const auto *Base : llvm::make_range(Bases.rbegin(), Bases.rend())) { 1003 Ptr = createArgument(Arg, Attr, Base).release(); 1004 if (Ptr) 1005 break; 1006 } 1007 } 1008 1009 if (Ptr && Arg.getValueAsBit("Optional")) 1010 Ptr->setOptional(true); 1011 1012 return std::unique_ptr<Argument>(Ptr); 1013 } 1014 1015 static void writeAvailabilityValue(raw_ostream &OS) { 1016 OS << "\" << getPlatform()->getName();\n" 1017 << " if (!getIntroduced().empty()) OS << \", introduced=\" << getIntroduced();\n" 1018 << " if (!getDeprecated().empty()) OS << \", deprecated=\" << getDeprecated();\n" 1019 << " if (!getObsoleted().empty()) OS << \", obsoleted=\" << getObsoleted();\n" 1020 << " if (getUnavailable()) OS << \", unavailable\";\n" 1021 << " OS << \""; 1022 } 1023 1024 static void writeGetSpellingFunction(Record &R, raw_ostream &OS) { 1025 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R); 1026 1027 OS << "const char *" << R.getName() << "Attr::getSpelling() const {\n"; 1028 if (Spellings.empty()) { 1029 OS << " return \"(No spelling)\";\n}\n\n"; 1030 return; 1031 } 1032 1033 OS << " switch (SpellingListIndex) {\n" 1034 " default:\n" 1035 " llvm_unreachable(\"Unknown attribute spelling!\");\n" 1036 " return \"(No spelling)\";\n"; 1037 1038 for (unsigned I = 0; I < Spellings.size(); ++I) 1039 OS << " case " << I << ":\n" 1040 " return \"" << Spellings[I].name() << "\";\n"; 1041 // End of the switch statement. 1042 OS << " }\n"; 1043 // End of the getSpelling function. 1044 OS << "}\n\n"; 1045 } 1046 1047 static void 1048 writePrettyPrintFunction(Record &R, 1049 const std::vector<std::unique_ptr<Argument>> &Args, 1050 raw_ostream &OS) { 1051 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R); 1052 1053 OS << "void " << R.getName() << "Attr::printPretty(" 1054 << "raw_ostream &OS, const PrintingPolicy &Policy) const {\n"; 1055 1056 if (Spellings.size() == 0) { 1057 OS << "}\n\n"; 1058 return; 1059 } 1060 1061 OS << 1062 " switch (SpellingListIndex) {\n" 1063 " default:\n" 1064 " llvm_unreachable(\"Unknown attribute spelling!\");\n" 1065 " break;\n"; 1066 1067 for (unsigned I = 0; I < Spellings.size(); ++ I) { 1068 llvm::SmallString<16> Prefix; 1069 llvm::SmallString<8> Suffix; 1070 // The actual spelling of the name and namespace (if applicable) 1071 // of an attribute without considering prefix and suffix. 1072 llvm::SmallString<64> Spelling; 1073 std::string Name = Spellings[I].name(); 1074 std::string Variety = Spellings[I].variety(); 1075 1076 if (Variety == "GNU") { 1077 Prefix = " __attribute__(("; 1078 Suffix = "))"; 1079 } else if (Variety == "CXX11") { 1080 Prefix = " [["; 1081 Suffix = "]]"; 1082 std::string Namespace = Spellings[I].nameSpace(); 1083 if (Namespace != "") { 1084 Spelling += Namespace; 1085 Spelling += "::"; 1086 } 1087 } else if (Variety == "Declspec") { 1088 Prefix = " __declspec("; 1089 Suffix = ")"; 1090 } else if (Variety == "Keyword") { 1091 Prefix = " "; 1092 Suffix = ""; 1093 } else { 1094 llvm_unreachable("Unknown attribute syntax variety!"); 1095 } 1096 1097 Spelling += Name; 1098 1099 OS << 1100 " case " << I << " : {\n" 1101 " OS << \"" + Prefix.str() + Spelling.str(); 1102 1103 if (!Args.empty()) 1104 OS << "("; 1105 if (Spelling == "availability") { 1106 writeAvailabilityValue(OS); 1107 } else { 1108 for (auto I = Args.begin(), E = Args.end(); I != E; ++ I) { 1109 if (I != Args.begin()) OS << ", "; 1110 (*I)->writeValue(OS); 1111 } 1112 } 1113 1114 if (!Args.empty()) 1115 OS << ")"; 1116 OS << Suffix.str() + "\";\n"; 1117 1118 OS << 1119 " break;\n" 1120 " }\n"; 1121 } 1122 1123 // End of the switch statement. 1124 OS << "}\n"; 1125 // End of the print function. 1126 OS << "}\n\n"; 1127 } 1128 1129 /// \brief Return the index of a spelling in a spelling list. 1130 static unsigned 1131 getSpellingListIndex(const std::vector<FlattenedSpelling> &SpellingList, 1132 const FlattenedSpelling &Spelling) { 1133 assert(SpellingList.size() && "Spelling list is empty!"); 1134 1135 for (unsigned Index = 0; Index < SpellingList.size(); ++Index) { 1136 const FlattenedSpelling &S = SpellingList[Index]; 1137 if (S.variety() != Spelling.variety()) 1138 continue; 1139 if (S.nameSpace() != Spelling.nameSpace()) 1140 continue; 1141 if (S.name() != Spelling.name()) 1142 continue; 1143 1144 return Index; 1145 } 1146 1147 llvm_unreachable("Unknown spelling!"); 1148 } 1149 1150 static void writeAttrAccessorDefinition(const Record &R, raw_ostream &OS) { 1151 std::vector<Record*> Accessors = R.getValueAsListOfDefs("Accessors"); 1152 for (const auto *Accessor : Accessors) { 1153 std::string Name = Accessor->getValueAsString("Name"); 1154 std::vector<FlattenedSpelling> Spellings = 1155 GetFlattenedSpellings(*Accessor); 1156 std::vector<FlattenedSpelling> SpellingList = GetFlattenedSpellings(R); 1157 assert(SpellingList.size() && 1158 "Attribute with empty spelling list can't have accessors!"); 1159 1160 OS << " bool " << Name << "() const { return SpellingListIndex == "; 1161 for (unsigned Index = 0; Index < Spellings.size(); ++Index) { 1162 OS << getSpellingListIndex(SpellingList, Spellings[Index]); 1163 if (Index != Spellings.size() -1) 1164 OS << " ||\n SpellingListIndex == "; 1165 else 1166 OS << "; }\n"; 1167 } 1168 } 1169 } 1170 1171 static bool 1172 SpellingNamesAreCommon(const std::vector<FlattenedSpelling>& Spellings) { 1173 assert(!Spellings.empty() && "An empty list of spellings was provided"); 1174 std::string FirstName = NormalizeNameForSpellingComparison( 1175 Spellings.front().name()); 1176 for (const auto &Spelling : 1177 llvm::make_range(std::next(Spellings.begin()), Spellings.end())) { 1178 std::string Name = NormalizeNameForSpellingComparison(Spelling.name()); 1179 if (Name != FirstName) 1180 return false; 1181 } 1182 return true; 1183 } 1184 1185 typedef std::map<unsigned, std::string> SemanticSpellingMap; 1186 static std::string 1187 CreateSemanticSpellings(const std::vector<FlattenedSpelling> &Spellings, 1188 SemanticSpellingMap &Map) { 1189 // The enumerants are automatically generated based on the variety, 1190 // namespace (if present) and name for each attribute spelling. However, 1191 // care is taken to avoid trampling on the reserved namespace due to 1192 // underscores. 1193 std::string Ret(" enum Spelling {\n"); 1194 std::set<std::string> Uniques; 1195 unsigned Idx = 0; 1196 for (auto I = Spellings.begin(), E = Spellings.end(); I != E; ++I, ++Idx) { 1197 const FlattenedSpelling &S = *I; 1198 std::string Variety = S.variety(); 1199 std::string Spelling = S.name(); 1200 std::string Namespace = S.nameSpace(); 1201 std::string EnumName = ""; 1202 1203 EnumName += (Variety + "_"); 1204 if (!Namespace.empty()) 1205 EnumName += (NormalizeNameForSpellingComparison(Namespace).str() + 1206 "_"); 1207 EnumName += NormalizeNameForSpellingComparison(Spelling); 1208 1209 // Even if the name is not unique, this spelling index corresponds to a 1210 // particular enumerant name that we've calculated. 1211 Map[Idx] = EnumName; 1212 1213 // Since we have been stripping underscores to avoid trampling on the 1214 // reserved namespace, we may have inadvertently created duplicate 1215 // enumerant names. These duplicates are not considered part of the 1216 // semantic spelling, and can be elided. 1217 if (Uniques.find(EnumName) != Uniques.end()) 1218 continue; 1219 1220 Uniques.insert(EnumName); 1221 if (I != Spellings.begin()) 1222 Ret += ",\n"; 1223 Ret += " " + EnumName; 1224 } 1225 Ret += "\n };\n\n"; 1226 return Ret; 1227 } 1228 1229 void WriteSemanticSpellingSwitch(const std::string &VarName, 1230 const SemanticSpellingMap &Map, 1231 raw_ostream &OS) { 1232 OS << " switch (" << VarName << ") {\n default: " 1233 << "llvm_unreachable(\"Unknown spelling list index\");\n"; 1234 for (const auto &I : Map) 1235 OS << " case " << I.first << ": return " << I.second << ";\n"; 1236 OS << " }\n"; 1237 } 1238 1239 // Emits the LateParsed property for attributes. 1240 static void emitClangAttrLateParsedList(RecordKeeper &Records, raw_ostream &OS) { 1241 OS << "#if defined(CLANG_ATTR_LATE_PARSED_LIST)\n"; 1242 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"); 1243 1244 for (const auto *Attr : Attrs) { 1245 bool LateParsed = Attr->getValueAsBit("LateParsed"); 1246 1247 if (LateParsed) { 1248 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*Attr); 1249 1250 // FIXME: Handle non-GNU attributes 1251 for (const auto &I : Spellings) { 1252 if (I.variety() != "GNU") 1253 continue; 1254 OS << ".Case(\"" << I.name() << "\", " << LateParsed << ")\n"; 1255 } 1256 } 1257 } 1258 OS << "#endif // CLANG_ATTR_LATE_PARSED_LIST\n\n"; 1259 } 1260 1261 /// \brief Emits the first-argument-is-type property for attributes. 1262 static void emitClangAttrTypeArgList(RecordKeeper &Records, raw_ostream &OS) { 1263 OS << "#if defined(CLANG_ATTR_TYPE_ARG_LIST)\n"; 1264 std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr"); 1265 1266 for (const auto *Attr : Attrs) { 1267 // Determine whether the first argument is a type. 1268 std::vector<Record *> Args = Attr->getValueAsListOfDefs("Args"); 1269 if (Args.empty()) 1270 continue; 1271 1272 if (Args[0]->getSuperClasses().back()->getName() != "TypeArgument") 1273 continue; 1274 1275 // All these spellings take a single type argument. 1276 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*Attr); 1277 std::set<std::string> Emitted; 1278 for (const auto &S : Spellings) { 1279 if (Emitted.insert(S.name()).second) 1280 OS << ".Case(\"" << S.name() << "\", " << "true" << ")\n"; 1281 } 1282 } 1283 OS << "#endif // CLANG_ATTR_TYPE_ARG_LIST\n\n"; 1284 } 1285 1286 /// \brief Emits the parse-arguments-in-unevaluated-context property for 1287 /// attributes. 1288 static void emitClangAttrArgContextList(RecordKeeper &Records, raw_ostream &OS) { 1289 OS << "#if defined(CLANG_ATTR_ARG_CONTEXT_LIST)\n"; 1290 ParsedAttrMap Attrs = getParsedAttrList(Records); 1291 for (const auto &I : Attrs) { 1292 const Record &Attr = *I.second; 1293 1294 if (!Attr.getValueAsBit("ParseArgumentsAsUnevaluated")) 1295 continue; 1296 1297 // All these spellings take are parsed unevaluated. 1298 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(Attr); 1299 std::set<std::string> Emitted; 1300 for (const auto &S : Spellings) { 1301 if (Emitted.insert(S.name()).second) 1302 OS << ".Case(\"" << S.name() << "\", " << "true" << ")\n"; 1303 } 1304 } 1305 OS << "#endif // CLANG_ATTR_ARG_CONTEXT_LIST\n\n"; 1306 } 1307 1308 static bool isIdentifierArgument(Record *Arg) { 1309 return !Arg->getSuperClasses().empty() && 1310 llvm::StringSwitch<bool>(Arg->getSuperClasses().back()->getName()) 1311 .Case("IdentifierArgument", true) 1312 .Case("EnumArgument", true) 1313 .Default(false); 1314 } 1315 1316 // Emits the first-argument-is-identifier property for attributes. 1317 static void emitClangAttrIdentifierArgList(RecordKeeper &Records, raw_ostream &OS) { 1318 OS << "#if defined(CLANG_ATTR_IDENTIFIER_ARG_LIST)\n"; 1319 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"); 1320 1321 for (const auto *Attr : Attrs) { 1322 // Determine whether the first argument is an identifier. 1323 std::vector<Record *> Args = Attr->getValueAsListOfDefs("Args"); 1324 if (Args.empty() || !isIdentifierArgument(Args[0])) 1325 continue; 1326 1327 // All these spellings take an identifier argument. 1328 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*Attr); 1329 std::set<std::string> Emitted; 1330 for (const auto &S : Spellings) { 1331 if (Emitted.insert(S.name()).second) 1332 OS << ".Case(\"" << S.name() << "\", " << "true" << ")\n"; 1333 } 1334 } 1335 OS << "#endif // CLANG_ATTR_IDENTIFIER_ARG_LIST\n\n"; 1336 } 1337 1338 namespace clang { 1339 1340 // Emits the class definitions for attributes. 1341 void EmitClangAttrClass(RecordKeeper &Records, raw_ostream &OS) { 1342 emitSourceFileHeader("Attribute classes' definitions", OS); 1343 1344 OS << "#ifndef LLVM_CLANG_ATTR_CLASSES_INC\n"; 1345 OS << "#define LLVM_CLANG_ATTR_CLASSES_INC\n\n"; 1346 1347 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"); 1348 1349 for (const auto *Attr : Attrs) { 1350 const Record &R = *Attr; 1351 1352 // FIXME: Currently, documentation is generated as-needed due to the fact 1353 // that there is no way to allow a generated project "reach into" the docs 1354 // directory (for instance, it may be an out-of-tree build). However, we want 1355 // to ensure that every attribute has a Documentation field, and produce an 1356 // error if it has been neglected. Otherwise, the on-demand generation which 1357 // happens server-side will fail. This code is ensuring that functionality, 1358 // even though this Emitter doesn't technically need the documentation. 1359 // When attribute documentation can be generated as part of the build 1360 // itself, this code can be removed. 1361 (void)R.getValueAsListOfDefs("Documentation"); 1362 1363 if (!R.getValueAsBit("ASTNode")) 1364 continue; 1365 1366 const std::vector<Record *> Supers = R.getSuperClasses(); 1367 assert(!Supers.empty() && "Forgot to specify a superclass for the attr"); 1368 std::string SuperName; 1369 for (const auto *Super : llvm::make_range(Supers.rbegin(), Supers.rend())) { 1370 const Record &R = *Super; 1371 if (R.getName() != "TargetSpecificAttr" && SuperName.empty()) 1372 SuperName = R.getName(); 1373 } 1374 1375 OS << "class " << R.getName() << "Attr : public " << SuperName << " {\n"; 1376 1377 std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args"); 1378 std::vector<std::unique_ptr<Argument>> Args; 1379 Args.reserve(ArgRecords.size()); 1380 1381 for (const auto *ArgRecord : ArgRecords) { 1382 Args.emplace_back(createArgument(*ArgRecord, R.getName())); 1383 Args.back()->writeDeclarations(OS); 1384 OS << "\n\n"; 1385 } 1386 1387 OS << "\npublic:\n"; 1388 1389 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R); 1390 1391 // If there are zero or one spellings, all spelling-related functionality 1392 // can be elided. If all of the spellings share the same name, the spelling 1393 // functionality can also be elided. 1394 bool ElideSpelling = (Spellings.size() <= 1) || 1395 SpellingNamesAreCommon(Spellings); 1396 1397 // This maps spelling index values to semantic Spelling enumerants. 1398 SemanticSpellingMap SemanticToSyntacticMap; 1399 1400 if (!ElideSpelling) 1401 OS << CreateSemanticSpellings(Spellings, SemanticToSyntacticMap); 1402 1403 OS << " static " << R.getName() << "Attr *CreateImplicit("; 1404 OS << "ASTContext &Ctx"; 1405 if (!ElideSpelling) 1406 OS << ", Spelling S"; 1407 for (auto const &ai : Args) { 1408 OS << ", "; 1409 ai->writeCtorParameters(OS); 1410 } 1411 OS << ", SourceRange Loc = SourceRange()"; 1412 OS << ") {\n"; 1413 OS << " " << R.getName() << "Attr *A = new (Ctx) " << R.getName(); 1414 OS << "Attr(Loc, Ctx, "; 1415 for (auto const &ai : Args) { 1416 ai->writeImplicitCtorArgs(OS); 1417 OS << ", "; 1418 } 1419 OS << (ElideSpelling ? "0" : "S") << ");\n"; 1420 OS << " A->setImplicit(true);\n"; 1421 OS << " return A;\n }\n\n"; 1422 1423 OS << " " << R.getName() << "Attr(SourceRange R, ASTContext &Ctx\n"; 1424 1425 bool HasOpt = false; 1426 for (auto const &ai : Args) { 1427 OS << " , "; 1428 ai->writeCtorParameters(OS); 1429 OS << "\n"; 1430 if (ai->isOptional()) 1431 HasOpt = true; 1432 } 1433 1434 OS << " , "; 1435 OS << "unsigned SI\n"; 1436 1437 OS << " )\n"; 1438 OS << " : " << SuperName << "(attr::" << R.getName() << ", R, SI)\n"; 1439 1440 for (auto const &ai : Args) { 1441 OS << " , "; 1442 ai->writeCtorInitializers(OS); 1443 OS << "\n"; 1444 } 1445 1446 OS << " {\n"; 1447 1448 for (auto const &ai : Args) { 1449 ai->writeCtorBody(OS); 1450 OS << "\n"; 1451 } 1452 OS << " }\n\n"; 1453 1454 // If there are optional arguments, write out a constructor that elides the 1455 // optional arguments as well. 1456 if (HasOpt) { 1457 OS << " " << R.getName() << "Attr(SourceRange R, ASTContext &Ctx\n"; 1458 for (auto const &ai : Args) { 1459 if (!ai->isOptional()) { 1460 OS << " , "; 1461 ai->writeCtorParameters(OS); 1462 OS << "\n"; 1463 } 1464 } 1465 1466 OS << " , "; 1467 OS << "unsigned SI\n"; 1468 1469 OS << " )\n"; 1470 OS << " : " << SuperName << "(attr::" << R.getName() << ", R, SI)\n"; 1471 1472 for (auto const &ai : Args) { 1473 OS << " , "; 1474 ai->writeCtorDefaultInitializers(OS); 1475 OS << "\n"; 1476 } 1477 1478 OS << " {\n"; 1479 1480 for (auto const &ai : Args) { 1481 if (!ai->isOptional()) { 1482 ai->writeCtorBody(OS); 1483 OS << "\n"; 1484 } 1485 } 1486 OS << " }\n\n"; 1487 } 1488 1489 OS << " " << R.getName() << "Attr *clone(ASTContext &C) const override;\n"; 1490 OS << " void printPretty(raw_ostream &OS,\n" 1491 << " const PrintingPolicy &Policy) const override;\n"; 1492 OS << " const char *getSpelling() const override;\n"; 1493 1494 if (!ElideSpelling) { 1495 assert(!SemanticToSyntacticMap.empty() && "Empty semantic mapping list"); 1496 OS << " Spelling getSemanticSpelling() const {\n"; 1497 WriteSemanticSpellingSwitch("SpellingListIndex", SemanticToSyntacticMap, 1498 OS); 1499 OS << " }\n"; 1500 } 1501 1502 writeAttrAccessorDefinition(R, OS); 1503 1504 for (auto const &ai : Args) { 1505 ai->writeAccessors(OS); 1506 OS << "\n\n"; 1507 1508 if (ai->isEnumArg()) 1509 static_cast<const EnumArgument *>(ai.get())->writeConversion(OS); 1510 else if (ai->isVariadicEnumArg()) 1511 static_cast<const VariadicEnumArgument *>(ai.get()) 1512 ->writeConversion(OS); 1513 } 1514 1515 OS << R.getValueAsString("AdditionalMembers"); 1516 OS << "\n\n"; 1517 1518 OS << " static bool classof(const Attr *A) { return A->getKind() == " 1519 << "attr::" << R.getName() << "; }\n"; 1520 1521 bool LateParsed = R.getValueAsBit("LateParsed"); 1522 OS << " bool isLateParsed() const override { return " 1523 << LateParsed << "; }\n"; 1524 1525 if (R.getValueAsBit("DuplicatesAllowedWhileMerging")) 1526 OS << " bool duplicatesAllowed() const override { return true; }\n\n"; 1527 1528 OS << "};\n\n"; 1529 } 1530 1531 OS << "#endif\n"; 1532 } 1533 1534 // Emits the class method definitions for attributes. 1535 void EmitClangAttrImpl(RecordKeeper &Records, raw_ostream &OS) { 1536 emitSourceFileHeader("Attribute classes' member function definitions", OS); 1537 1538 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"); 1539 1540 for (auto *Attr : Attrs) { 1541 Record &R = *Attr; 1542 1543 if (!R.getValueAsBit("ASTNode")) 1544 continue; 1545 1546 std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args"); 1547 std::vector<std::unique_ptr<Argument>> Args; 1548 for (const auto *Arg : ArgRecords) 1549 Args.emplace_back(createArgument(*Arg, R.getName())); 1550 1551 for (auto const &ai : Args) 1552 ai->writeAccessorDefinitions(OS); 1553 1554 OS << R.getName() << "Attr *" << R.getName() 1555 << "Attr::clone(ASTContext &C) const {\n"; 1556 OS << " auto *A = new (C) " << R.getName() << "Attr(getLocation(), C"; 1557 for (auto const &ai : Args) { 1558 OS << ", "; 1559 ai->writeCloneArgs(OS); 1560 } 1561 OS << ", getSpellingListIndex());\n"; 1562 OS << " A->Inherited = Inherited;\n"; 1563 OS << " A->IsPackExpansion = IsPackExpansion;\n"; 1564 OS << " A->Implicit = Implicit;\n"; 1565 OS << " return A;\n}\n\n"; 1566 1567 writePrettyPrintFunction(R, Args, OS); 1568 writeGetSpellingFunction(R, OS); 1569 } 1570 } 1571 1572 } // end namespace clang 1573 1574 static void EmitAttrList(raw_ostream &OS, StringRef Class, 1575 const std::vector<Record*> &AttrList) { 1576 std::vector<Record*>::const_iterator i = AttrList.begin(), e = AttrList.end(); 1577 1578 if (i != e) { 1579 // Move the end iterator back to emit the last attribute. 1580 for(--e; i != e; ++i) { 1581 if (!(*i)->getValueAsBit("ASTNode")) 1582 continue; 1583 1584 OS << Class << "(" << (*i)->getName() << ")\n"; 1585 } 1586 1587 OS << "LAST_" << Class << "(" << (*i)->getName() << ")\n\n"; 1588 } 1589 } 1590 1591 namespace clang { 1592 1593 // Emits the enumeration list for attributes. 1594 void EmitClangAttrList(RecordKeeper &Records, raw_ostream &OS) { 1595 emitSourceFileHeader("List of all attributes that Clang recognizes", OS); 1596 1597 OS << "#ifndef LAST_ATTR\n"; 1598 OS << "#define LAST_ATTR(NAME) ATTR(NAME)\n"; 1599 OS << "#endif\n\n"; 1600 1601 OS << "#ifndef INHERITABLE_ATTR\n"; 1602 OS << "#define INHERITABLE_ATTR(NAME) ATTR(NAME)\n"; 1603 OS << "#endif\n\n"; 1604 1605 OS << "#ifndef LAST_INHERITABLE_ATTR\n"; 1606 OS << "#define LAST_INHERITABLE_ATTR(NAME) INHERITABLE_ATTR(NAME)\n"; 1607 OS << "#endif\n\n"; 1608 1609 OS << "#ifndef INHERITABLE_PARAM_ATTR\n"; 1610 OS << "#define INHERITABLE_PARAM_ATTR(NAME) ATTR(NAME)\n"; 1611 OS << "#endif\n\n"; 1612 1613 OS << "#ifndef LAST_INHERITABLE_PARAM_ATTR\n"; 1614 OS << "#define LAST_INHERITABLE_PARAM_ATTR(NAME)" 1615 " INHERITABLE_PARAM_ATTR(NAME)\n"; 1616 OS << "#endif\n\n"; 1617 1618 Record *InhClass = Records.getClass("InheritableAttr"); 1619 Record *InhParamClass = Records.getClass("InheritableParamAttr"); 1620 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"), 1621 NonInhAttrs, InhAttrs, InhParamAttrs; 1622 for (auto *Attr : Attrs) { 1623 if (!Attr->getValueAsBit("ASTNode")) 1624 continue; 1625 1626 if (Attr->isSubClassOf(InhParamClass)) 1627 InhParamAttrs.push_back(Attr); 1628 else if (Attr->isSubClassOf(InhClass)) 1629 InhAttrs.push_back(Attr); 1630 else 1631 NonInhAttrs.push_back(Attr); 1632 } 1633 1634 EmitAttrList(OS, "INHERITABLE_PARAM_ATTR", InhParamAttrs); 1635 EmitAttrList(OS, "INHERITABLE_ATTR", InhAttrs); 1636 EmitAttrList(OS, "ATTR", NonInhAttrs); 1637 1638 OS << "#undef LAST_ATTR\n"; 1639 OS << "#undef INHERITABLE_ATTR\n"; 1640 OS << "#undef LAST_INHERITABLE_ATTR\n"; 1641 OS << "#undef LAST_INHERITABLE_PARAM_ATTR\n"; 1642 OS << "#undef ATTR\n"; 1643 } 1644 1645 // Emits the code to read an attribute from a precompiled header. 1646 void EmitClangAttrPCHRead(RecordKeeper &Records, raw_ostream &OS) { 1647 emitSourceFileHeader("Attribute deserialization code", OS); 1648 1649 Record *InhClass = Records.getClass("InheritableAttr"); 1650 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"), 1651 ArgRecords; 1652 std::vector<std::unique_ptr<Argument>> Args; 1653 1654 OS << " switch (Kind) {\n"; 1655 OS << " default:\n"; 1656 OS << " assert(0 && \"Unknown attribute!\");\n"; 1657 OS << " break;\n"; 1658 for (const auto *Attr : Attrs) { 1659 const Record &R = *Attr; 1660 if (!R.getValueAsBit("ASTNode")) 1661 continue; 1662 1663 OS << " case attr::" << R.getName() << ": {\n"; 1664 if (R.isSubClassOf(InhClass)) 1665 OS << " bool isInherited = Record[Idx++];\n"; 1666 OS << " bool isImplicit = Record[Idx++];\n"; 1667 OS << " unsigned Spelling = Record[Idx++];\n"; 1668 ArgRecords = R.getValueAsListOfDefs("Args"); 1669 Args.clear(); 1670 for (const auto *Arg : ArgRecords) { 1671 Args.emplace_back(createArgument(*Arg, R.getName())); 1672 Args.back()->writePCHReadDecls(OS); 1673 } 1674 OS << " New = new (Context) " << R.getName() << "Attr(Range, Context"; 1675 for (auto const &ri : Args) { 1676 OS << ", "; 1677 ri->writePCHReadArgs(OS); 1678 } 1679 OS << ", Spelling);\n"; 1680 if (R.isSubClassOf(InhClass)) 1681 OS << " cast<InheritableAttr>(New)->setInherited(isInherited);\n"; 1682 OS << " New->setImplicit(isImplicit);\n"; 1683 OS << " break;\n"; 1684 OS << " }\n"; 1685 } 1686 OS << " }\n"; 1687 } 1688 1689 // Emits the code to write an attribute to a precompiled header. 1690 void EmitClangAttrPCHWrite(RecordKeeper &Records, raw_ostream &OS) { 1691 emitSourceFileHeader("Attribute serialization code", OS); 1692 1693 Record *InhClass = Records.getClass("InheritableAttr"); 1694 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"), Args; 1695 1696 OS << " switch (A->getKind()) {\n"; 1697 OS << " default:\n"; 1698 OS << " llvm_unreachable(\"Unknown attribute kind!\");\n"; 1699 OS << " break;\n"; 1700 for (const auto *Attr : Attrs) { 1701 const Record &R = *Attr; 1702 if (!R.getValueAsBit("ASTNode")) 1703 continue; 1704 OS << " case attr::" << R.getName() << ": {\n"; 1705 Args = R.getValueAsListOfDefs("Args"); 1706 if (R.isSubClassOf(InhClass) || !Args.empty()) 1707 OS << " const " << R.getName() << "Attr *SA = cast<" << R.getName() 1708 << "Attr>(A);\n"; 1709 if (R.isSubClassOf(InhClass)) 1710 OS << " Record.push_back(SA->isInherited());\n"; 1711 OS << " Record.push_back(A->isImplicit());\n"; 1712 OS << " Record.push_back(A->getSpellingListIndex());\n"; 1713 1714 for (const auto *Arg : Args) 1715 createArgument(*Arg, R.getName())->writePCHWrite(OS); 1716 OS << " break;\n"; 1717 OS << " }\n"; 1718 } 1719 OS << " }\n"; 1720 } 1721 1722 static void GenerateHasAttrSpellingStringSwitch( 1723 const std::vector<Record *> &Attrs, raw_ostream &OS, 1724 const std::string &Variety = "", const std::string &Scope = "") { 1725 for (const auto *Attr : Attrs) { 1726 // It is assumed that there will be an llvm::Triple object named T within 1727 // scope that can be used to determine whether the attribute exists in 1728 // a given target. 1729 std::string Test; 1730 if (Attr->isSubClassOf("TargetSpecificAttr")) { 1731 const Record *R = Attr->getValueAsDef("Target"); 1732 std::vector<std::string> Arches = R->getValueAsListOfStrings("Arches"); 1733 1734 Test += "("; 1735 for (auto AI = Arches.begin(), AE = Arches.end(); AI != AE; ++AI) { 1736 std::string Part = *AI; 1737 Test += "T.getArch() == llvm::Triple::" + Part; 1738 if (AI + 1 != AE) 1739 Test += " || "; 1740 } 1741 Test += ")"; 1742 1743 std::vector<std::string> OSes; 1744 if (!R->isValueUnset("OSes")) { 1745 Test += " && ("; 1746 std::vector<std::string> OSes = R->getValueAsListOfStrings("OSes"); 1747 for (auto AI = OSes.begin(), AE = OSes.end(); AI != AE; ++AI) { 1748 std::string Part = *AI; 1749 1750 Test += "T.getOS() == llvm::Triple::" + Part; 1751 if (AI + 1 != AE) 1752 Test += " || "; 1753 } 1754 Test += ")"; 1755 } 1756 1757 // If this is the C++11 variety, also add in the LangOpts test. 1758 if (Variety == "CXX11") 1759 Test += " && LangOpts.CPlusPlus11"; 1760 } else if (Variety == "CXX11") 1761 // C++11 mode should be checked against LangOpts, which is presumed to be 1762 // present in the caller. 1763 Test = "LangOpts.CPlusPlus11"; 1764 else 1765 Test = "true"; 1766 1767 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*Attr); 1768 for (const auto &S : Spellings) 1769 if (Variety.empty() || (Variety == S.variety() && 1770 (Scope.empty() || Scope == S.nameSpace()))) 1771 OS << " .Case(\"" << S.name() << "\", " << Test << ")\n"; 1772 } 1773 OS << " .Default(false);\n"; 1774 } 1775 1776 // Emits the list of spellings for attributes. 1777 void EmitClangAttrHasAttrImpl(RecordKeeper &Records, raw_ostream &OS) { 1778 emitSourceFileHeader("Code to implement the __has_attribute logic", OS); 1779 1780 // Separate all of the attributes out into four group: generic, C++11, GNU, 1781 // and declspecs. Then generate a big switch statement for each of them. 1782 std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr"); 1783 std::vector<Record *> Declspec, GNU; 1784 std::map<std::string, std::vector<Record *>> CXX; 1785 1786 // Walk over the list of all attributes, and split them out based on the 1787 // spelling variety. 1788 for (auto *R : Attrs) { 1789 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*R); 1790 for (const auto &SI : Spellings) { 1791 std::string Variety = SI.variety(); 1792 if (Variety == "GNU") 1793 GNU.push_back(R); 1794 else if (Variety == "Declspec") 1795 Declspec.push_back(R); 1796 else if (Variety == "CXX11") { 1797 CXX[SI.nameSpace()].push_back(R); 1798 } 1799 } 1800 } 1801 1802 OS << "switch (Syntax) {\n"; 1803 OS << "case AttrSyntax::Generic:\n"; 1804 OS << " return llvm::StringSwitch<bool>(Name)\n"; 1805 GenerateHasAttrSpellingStringSwitch(Attrs, OS); 1806 OS << "case AttrSyntax::GNU:\n"; 1807 OS << " return llvm::StringSwitch<bool>(Name)\n"; 1808 GenerateHasAttrSpellingStringSwitch(GNU, OS, "GNU"); 1809 OS << "case AttrSyntax::Declspec:\n"; 1810 OS << " return llvm::StringSwitch<bool>(Name)\n"; 1811 GenerateHasAttrSpellingStringSwitch(Declspec, OS, "Declspec"); 1812 OS << "case AttrSyntax::CXX: {\n"; 1813 // C++11-style attributes are further split out based on the Scope. 1814 for (std::map<std::string, std::vector<Record *>>::iterator I = CXX.begin(), 1815 E = CXX.end(); 1816 I != E; ++I) { 1817 if (I != CXX.begin()) 1818 OS << " else "; 1819 if (I->first.empty()) 1820 OS << "if (!Scope || Scope->getName() == \"\") {\n"; 1821 else 1822 OS << "if (Scope->getName() == \"" << I->first << "\") {\n"; 1823 OS << " return llvm::StringSwitch<bool>(Name)\n"; 1824 GenerateHasAttrSpellingStringSwitch(I->second, OS, "CXX11", I->first); 1825 OS << "}"; 1826 } 1827 OS << "\n}\n"; 1828 OS << "}\n"; 1829 } 1830 1831 void EmitClangAttrSpellingListIndex(RecordKeeper &Records, raw_ostream &OS) { 1832 emitSourceFileHeader("Code to translate different attribute spellings " 1833 "into internal identifiers", OS); 1834 1835 OS << 1836 " switch (AttrKind) {\n" 1837 " default:\n" 1838 " llvm_unreachable(\"Unknown attribute kind!\");\n" 1839 " break;\n"; 1840 1841 ParsedAttrMap Attrs = getParsedAttrList(Records); 1842 for (const auto &I : Attrs) { 1843 const Record &R = *I.second; 1844 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R); 1845 OS << " case AT_" << I.first << ": {\n"; 1846 for (unsigned I = 0; I < Spellings.size(); ++ I) { 1847 OS << " if (Name == \"" 1848 << Spellings[I].name() << "\" && " 1849 << "SyntaxUsed == " 1850 << StringSwitch<unsigned>(Spellings[I].variety()) 1851 .Case("GNU", 0) 1852 .Case("CXX11", 1) 1853 .Case("Declspec", 2) 1854 .Case("Keyword", 3) 1855 .Default(0) 1856 << " && Scope == \"" << Spellings[I].nameSpace() << "\")\n" 1857 << " return " << I << ";\n"; 1858 } 1859 1860 OS << " break;\n"; 1861 OS << " }\n"; 1862 } 1863 1864 OS << " }\n"; 1865 OS << " return 0;\n"; 1866 } 1867 1868 // Emits code used by RecursiveASTVisitor to visit attributes 1869 void EmitClangAttrASTVisitor(RecordKeeper &Records, raw_ostream &OS) { 1870 emitSourceFileHeader("Used by RecursiveASTVisitor to visit attributes.", OS); 1871 1872 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"); 1873 1874 // Write method declarations for Traverse* methods. 1875 // We emit this here because we only generate methods for attributes that 1876 // are declared as ASTNodes. 1877 OS << "#ifdef ATTR_VISITOR_DECLS_ONLY\n\n"; 1878 for (const auto *Attr : Attrs) { 1879 const Record &R = *Attr; 1880 if (!R.getValueAsBit("ASTNode")) 1881 continue; 1882 OS << " bool Traverse" 1883 << R.getName() << "Attr(" << R.getName() << "Attr *A);\n"; 1884 OS << " bool Visit" 1885 << R.getName() << "Attr(" << R.getName() << "Attr *A) {\n" 1886 << " return true; \n" 1887 << " };\n"; 1888 } 1889 OS << "\n#else // ATTR_VISITOR_DECLS_ONLY\n\n"; 1890 1891 // Write individual Traverse* methods for each attribute class. 1892 for (const auto *Attr : Attrs) { 1893 const Record &R = *Attr; 1894 if (!R.getValueAsBit("ASTNode")) 1895 continue; 1896 1897 OS << "template <typename Derived>\n" 1898 << "bool VISITORCLASS<Derived>::Traverse" 1899 << R.getName() << "Attr(" << R.getName() << "Attr *A) {\n" 1900 << " if (!getDerived().VisitAttr(A))\n" 1901 << " return false;\n" 1902 << " if (!getDerived().Visit" << R.getName() << "Attr(A))\n" 1903 << " return false;\n"; 1904 1905 std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args"); 1906 for (const auto *Arg : ArgRecords) 1907 createArgument(*Arg, R.getName())->writeASTVisitorTraversal(OS); 1908 1909 OS << " return true;\n"; 1910 OS << "}\n\n"; 1911 } 1912 1913 // Write generic Traverse routine 1914 OS << "template <typename Derived>\n" 1915 << "bool VISITORCLASS<Derived>::TraverseAttr(Attr *A) {\n" 1916 << " if (!A)\n" 1917 << " return true;\n" 1918 << "\n" 1919 << " switch (A->getKind()) {\n" 1920 << " default:\n" 1921 << " return true;\n"; 1922 1923 for (const auto *Attr : Attrs) { 1924 const Record &R = *Attr; 1925 if (!R.getValueAsBit("ASTNode")) 1926 continue; 1927 1928 OS << " case attr::" << R.getName() << ":\n" 1929 << " return getDerived().Traverse" << R.getName() << "Attr(" 1930 << "cast<" << R.getName() << "Attr>(A));\n"; 1931 } 1932 OS << " }\n"; // end case 1933 OS << "}\n"; // end function 1934 OS << "#endif // ATTR_VISITOR_DECLS_ONLY\n"; 1935 } 1936 1937 // Emits code to instantiate dependent attributes on templates. 1938 void EmitClangAttrTemplateInstantiate(RecordKeeper &Records, raw_ostream &OS) { 1939 emitSourceFileHeader("Template instantiation code for attributes", OS); 1940 1941 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"); 1942 1943 OS << "namespace clang {\n" 1944 << "namespace sema {\n\n" 1945 << "Attr *instantiateTemplateAttribute(const Attr *At, ASTContext &C, " 1946 << "Sema &S,\n" 1947 << " const MultiLevelTemplateArgumentList &TemplateArgs) {\n" 1948 << " switch (At->getKind()) {\n" 1949 << " default:\n" 1950 << " break;\n"; 1951 1952 for (const auto *Attr : Attrs) { 1953 const Record &R = *Attr; 1954 if (!R.getValueAsBit("ASTNode")) 1955 continue; 1956 1957 OS << " case attr::" << R.getName() << ": {\n"; 1958 bool ShouldClone = R.getValueAsBit("Clone"); 1959 1960 if (!ShouldClone) { 1961 OS << " return NULL;\n"; 1962 OS << " }\n"; 1963 continue; 1964 } 1965 1966 OS << " const " << R.getName() << "Attr *A = cast<" 1967 << R.getName() << "Attr>(At);\n"; 1968 bool TDependent = R.getValueAsBit("TemplateDependent"); 1969 1970 if (!TDependent) { 1971 OS << " return A->clone(C);\n"; 1972 OS << " }\n"; 1973 continue; 1974 } 1975 1976 std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args"); 1977 std::vector<std::unique_ptr<Argument>> Args; 1978 Args.reserve(ArgRecords.size()); 1979 1980 for (const auto *ArgRecord : ArgRecords) 1981 Args.emplace_back(createArgument(*ArgRecord, R.getName())); 1982 1983 for (auto const &ai : Args) 1984 ai->writeTemplateInstantiation(OS); 1985 1986 OS << " return new (C) " << R.getName() << "Attr(A->getLocation(), C"; 1987 for (auto const &ai : Args) { 1988 OS << ", "; 1989 ai->writeTemplateInstantiationArgs(OS); 1990 } 1991 OS << ", A->getSpellingListIndex());\n }\n"; 1992 } 1993 OS << " } // end switch\n" 1994 << " llvm_unreachable(\"Unknown attribute!\");\n" 1995 << " return 0;\n" 1996 << "}\n\n" 1997 << "} // end namespace sema\n" 1998 << "} // end namespace clang\n"; 1999 } 2000 2001 // Emits the list of parsed attributes. 2002 void EmitClangAttrParsedAttrList(RecordKeeper &Records, raw_ostream &OS) { 2003 emitSourceFileHeader("List of all attributes that Clang recognizes", OS); 2004 2005 OS << "#ifndef PARSED_ATTR\n"; 2006 OS << "#define PARSED_ATTR(NAME) NAME\n"; 2007 OS << "#endif\n\n"; 2008 2009 ParsedAttrMap Names = getParsedAttrList(Records); 2010 for (const auto &I : Names) { 2011 OS << "PARSED_ATTR(" << I.first << ")\n"; 2012 } 2013 } 2014 2015 static void emitArgInfo(const Record &R, std::stringstream &OS) { 2016 // This function will count the number of arguments specified for the 2017 // attribute and emit the number of required arguments followed by the 2018 // number of optional arguments. 2019 std::vector<Record *> Args = R.getValueAsListOfDefs("Args"); 2020 unsigned ArgCount = 0, OptCount = 0; 2021 for (const auto *Arg : Args) { 2022 Arg->getValueAsBit("Optional") ? ++OptCount : ++ArgCount; 2023 } 2024 OS << ArgCount << ", " << OptCount; 2025 } 2026 2027 static void GenerateDefaultAppertainsTo(raw_ostream &OS) { 2028 OS << "static bool defaultAppertainsTo(Sema &, const AttributeList &,"; 2029 OS << "const Decl *) {\n"; 2030 OS << " return true;\n"; 2031 OS << "}\n\n"; 2032 } 2033 2034 static std::string CalculateDiagnostic(const Record &S) { 2035 // If the SubjectList object has a custom diagnostic associated with it, 2036 // return that directly. 2037 std::string CustomDiag = S.getValueAsString("CustomDiag"); 2038 if (!CustomDiag.empty()) 2039 return CustomDiag; 2040 2041 // Given the list of subjects, determine what diagnostic best fits. 2042 enum { 2043 Func = 1U << 0, 2044 Var = 1U << 1, 2045 ObjCMethod = 1U << 2, 2046 Param = 1U << 3, 2047 Class = 1U << 4, 2048 GenericRecord = 1U << 5, 2049 Type = 1U << 6, 2050 ObjCIVar = 1U << 7, 2051 ObjCProp = 1U << 8, 2052 ObjCInterface = 1U << 9, 2053 Block = 1U << 10, 2054 Namespace = 1U << 11, 2055 Field = 1U << 12, 2056 CXXMethod = 1U << 13, 2057 ObjCProtocol = 1U << 14 2058 }; 2059 uint32_t SubMask = 0; 2060 2061 std::vector<Record *> Subjects = S.getValueAsListOfDefs("Subjects"); 2062 for (const auto *Subject : Subjects) { 2063 const Record &R = *Subject; 2064 std::string Name; 2065 2066 if (R.isSubClassOf("SubsetSubject")) { 2067 PrintError(R.getLoc(), "SubsetSubjects should use a custom diagnostic"); 2068 // As a fallback, look through the SubsetSubject to see what its base 2069 // type is, and use that. This needs to be updated if SubsetSubjects 2070 // are allowed within other SubsetSubjects. 2071 Name = R.getValueAsDef("Base")->getName(); 2072 } else 2073 Name = R.getName(); 2074 2075 uint32_t V = StringSwitch<uint32_t>(Name) 2076 .Case("Function", Func) 2077 .Case("Var", Var) 2078 .Case("ObjCMethod", ObjCMethod) 2079 .Case("ParmVar", Param) 2080 .Case("TypedefName", Type) 2081 .Case("ObjCIvar", ObjCIVar) 2082 .Case("ObjCProperty", ObjCProp) 2083 .Case("Record", GenericRecord) 2084 .Case("ObjCInterface", ObjCInterface) 2085 .Case("ObjCProtocol", ObjCProtocol) 2086 .Case("Block", Block) 2087 .Case("CXXRecord", Class) 2088 .Case("Namespace", Namespace) 2089 .Case("Field", Field) 2090 .Case("CXXMethod", CXXMethod) 2091 .Default(0); 2092 if (!V) { 2093 // Something wasn't in our mapping, so be helpful and let the developer 2094 // know about it. 2095 PrintFatalError(R.getLoc(), "Unknown subject type: " + R.getName()); 2096 return ""; 2097 } 2098 2099 SubMask |= V; 2100 } 2101 2102 switch (SubMask) { 2103 // For the simple cases where there's only a single entry in the mask, we 2104 // don't have to resort to bit fiddling. 2105 case Func: return "ExpectedFunction"; 2106 case Var: return "ExpectedVariable"; 2107 case Param: return "ExpectedParameter"; 2108 case Class: return "ExpectedClass"; 2109 case CXXMethod: 2110 // FIXME: Currently, this maps to ExpectedMethod based on existing code, 2111 // but should map to something a bit more accurate at some point. 2112 case ObjCMethod: return "ExpectedMethod"; 2113 case Type: return "ExpectedType"; 2114 case ObjCInterface: return "ExpectedObjectiveCInterface"; 2115 case ObjCProtocol: return "ExpectedObjectiveCProtocol"; 2116 2117 // "GenericRecord" means struct, union or class; check the language options 2118 // and if not compiling for C++, strip off the class part. Note that this 2119 // relies on the fact that the context for this declares "Sema &S". 2120 case GenericRecord: 2121 return "(S.getLangOpts().CPlusPlus ? ExpectedStructOrUnionOrClass : " 2122 "ExpectedStructOrUnion)"; 2123 case Func | ObjCMethod | Block: return "ExpectedFunctionMethodOrBlock"; 2124 case Func | ObjCMethod | Class: return "ExpectedFunctionMethodOrClass"; 2125 case Func | Param: 2126 case Func | ObjCMethod | Param: return "ExpectedFunctionMethodOrParameter"; 2127 case Func | ObjCMethod: return "ExpectedFunctionOrMethod"; 2128 case Func | Var: return "ExpectedVariableOrFunction"; 2129 2130 // If not compiling for C++, the class portion does not apply. 2131 case Func | Var | Class: 2132 return "(S.getLangOpts().CPlusPlus ? ExpectedFunctionVariableOrClass : " 2133 "ExpectedVariableOrFunction)"; 2134 2135 case ObjCMethod | ObjCProp: return "ExpectedMethodOrProperty"; 2136 case Field | Var: return "ExpectedFieldOrGlobalVar"; 2137 } 2138 2139 PrintFatalError(S.getLoc(), 2140 "Could not deduce diagnostic argument for Attr subjects"); 2141 2142 return ""; 2143 } 2144 2145 static std::string GetSubjectWithSuffix(const Record *R) { 2146 std::string B = R->getName(); 2147 if (B == "DeclBase") 2148 return "Decl"; 2149 return B + "Decl"; 2150 } 2151 static std::string GenerateCustomAppertainsTo(const Record &Subject, 2152 raw_ostream &OS) { 2153 std::string FnName = "is" + Subject.getName(); 2154 2155 // If this code has already been generated, simply return the previous 2156 // instance of it. 2157 static std::set<std::string> CustomSubjectSet; 2158 std::set<std::string>::iterator I = CustomSubjectSet.find(FnName); 2159 if (I != CustomSubjectSet.end()) 2160 return *I; 2161 2162 Record *Base = Subject.getValueAsDef("Base"); 2163 2164 // Not currently support custom subjects within custom subjects. 2165 if (Base->isSubClassOf("SubsetSubject")) { 2166 PrintFatalError(Subject.getLoc(), 2167 "SubsetSubjects within SubsetSubjects is not supported"); 2168 return ""; 2169 } 2170 2171 OS << "static bool " << FnName << "(const Decl *D) {\n"; 2172 OS << " if (const " << GetSubjectWithSuffix(Base) << " *S = dyn_cast<"; 2173 OS << GetSubjectWithSuffix(Base); 2174 OS << ">(D))\n"; 2175 OS << " return " << Subject.getValueAsString("CheckCode") << ";\n"; 2176 OS << " return false;\n"; 2177 OS << "}\n\n"; 2178 2179 CustomSubjectSet.insert(FnName); 2180 return FnName; 2181 } 2182 2183 static std::string GenerateAppertainsTo(const Record &Attr, raw_ostream &OS) { 2184 // If the attribute does not contain a Subjects definition, then use the 2185 // default appertainsTo logic. 2186 if (Attr.isValueUnset("Subjects")) 2187 return "defaultAppertainsTo"; 2188 2189 const Record *SubjectObj = Attr.getValueAsDef("Subjects"); 2190 std::vector<Record*> Subjects = SubjectObj->getValueAsListOfDefs("Subjects"); 2191 2192 // If the list of subjects is empty, it is assumed that the attribute 2193 // appertains to everything. 2194 if (Subjects.empty()) 2195 return "defaultAppertainsTo"; 2196 2197 bool Warn = SubjectObj->getValueAsDef("Diag")->getValueAsBit("Warn"); 2198 2199 // Otherwise, generate an appertainsTo check specific to this attribute which 2200 // checks all of the given subjects against the Decl passed in. Return the 2201 // name of that check to the caller. 2202 std::string FnName = "check" + Attr.getName() + "AppertainsTo"; 2203 std::stringstream SS; 2204 SS << "static bool " << FnName << "(Sema &S, const AttributeList &Attr, "; 2205 SS << "const Decl *D) {\n"; 2206 SS << " if ("; 2207 for (auto I = Subjects.begin(), E = Subjects.end(); I != E; ++I) { 2208 // If the subject has custom code associated with it, generate a function 2209 // for it. The function cannot be inlined into this check (yet) because it 2210 // requires the subject to be of a specific type, and were that information 2211 // inlined here, it would not support an attribute with multiple custom 2212 // subjects. 2213 if ((*I)->isSubClassOf("SubsetSubject")) { 2214 SS << "!" << GenerateCustomAppertainsTo(**I, OS) << "(D)"; 2215 } else { 2216 SS << "!isa<" << GetSubjectWithSuffix(*I) << ">(D)"; 2217 } 2218 2219 if (I + 1 != E) 2220 SS << " && "; 2221 } 2222 SS << ") {\n"; 2223 SS << " S.Diag(Attr.getLoc(), diag::"; 2224 SS << (Warn ? "warn_attribute_wrong_decl_type" : 2225 "err_attribute_wrong_decl_type"); 2226 SS << ")\n"; 2227 SS << " << Attr.getName() << "; 2228 SS << CalculateDiagnostic(*SubjectObj) << ";\n"; 2229 SS << " return false;\n"; 2230 SS << " }\n"; 2231 SS << " return true;\n"; 2232 SS << "}\n\n"; 2233 2234 OS << SS.str(); 2235 return FnName; 2236 } 2237 2238 static void GenerateDefaultLangOptRequirements(raw_ostream &OS) { 2239 OS << "static bool defaultDiagnoseLangOpts(Sema &, "; 2240 OS << "const AttributeList &) {\n"; 2241 OS << " return true;\n"; 2242 OS << "}\n\n"; 2243 } 2244 2245 static std::string GenerateLangOptRequirements(const Record &R, 2246 raw_ostream &OS) { 2247 // If the attribute has an empty or unset list of language requirements, 2248 // return the default handler. 2249 std::vector<Record *> LangOpts = R.getValueAsListOfDefs("LangOpts"); 2250 if (LangOpts.empty()) 2251 return "defaultDiagnoseLangOpts"; 2252 2253 // Generate the test condition, as well as a unique function name for the 2254 // diagnostic test. The list of options should usually be short (one or two 2255 // options), and the uniqueness isn't strictly necessary (it is just for 2256 // codegen efficiency). 2257 std::string FnName = "check", Test; 2258 for (auto I = LangOpts.begin(), E = LangOpts.end(); I != E; ++I) { 2259 std::string Part = (*I)->getValueAsString("Name"); 2260 Test += "S.LangOpts." + Part; 2261 if (I + 1 != E) 2262 Test += " || "; 2263 FnName += Part; 2264 } 2265 FnName += "LangOpts"; 2266 2267 // If this code has already been generated, simply return the previous 2268 // instance of it. 2269 static std::set<std::string> CustomLangOptsSet; 2270 std::set<std::string>::iterator I = CustomLangOptsSet.find(FnName); 2271 if (I != CustomLangOptsSet.end()) 2272 return *I; 2273 2274 OS << "static bool " << FnName << "(Sema &S, const AttributeList &Attr) {\n"; 2275 OS << " if (" << Test << ")\n"; 2276 OS << " return true;\n\n"; 2277 OS << " S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) "; 2278 OS << "<< Attr.getName();\n"; 2279 OS << " return false;\n"; 2280 OS << "}\n\n"; 2281 2282 CustomLangOptsSet.insert(FnName); 2283 return FnName; 2284 } 2285 2286 static void GenerateDefaultTargetRequirements(raw_ostream &OS) { 2287 OS << "static bool defaultTargetRequirements(const llvm::Triple &) {\n"; 2288 OS << " return true;\n"; 2289 OS << "}\n\n"; 2290 } 2291 2292 static std::string GenerateTargetRequirements(const Record &Attr, 2293 const ParsedAttrMap &Dupes, 2294 raw_ostream &OS) { 2295 // If the attribute is not a target specific attribute, return the default 2296 // target handler. 2297 if (!Attr.isSubClassOf("TargetSpecificAttr")) 2298 return "defaultTargetRequirements"; 2299 2300 // Get the list of architectures to be tested for. 2301 const Record *R = Attr.getValueAsDef("Target"); 2302 std::vector<std::string> Arches = R->getValueAsListOfStrings("Arches"); 2303 if (Arches.empty()) { 2304 PrintError(Attr.getLoc(), "Empty list of target architectures for a " 2305 "target-specific attr"); 2306 return "defaultTargetRequirements"; 2307 } 2308 2309 // If there are other attributes which share the same parsed attribute kind, 2310 // such as target-specific attributes with a shared spelling, collapse the 2311 // duplicate architectures. This is required because a shared target-specific 2312 // attribute has only one AttributeList::Kind enumeration value, but it 2313 // applies to multiple target architectures. In order for the attribute to be 2314 // considered valid, all of its architectures need to be included. 2315 if (!Attr.isValueUnset("ParseKind")) { 2316 std::string APK = Attr.getValueAsString("ParseKind"); 2317 for (const auto &I : Dupes) { 2318 if (I.first == APK) { 2319 std::vector<std::string> DA = I.second->getValueAsDef("Target") 2320 ->getValueAsListOfStrings("Arches"); 2321 std::copy(DA.begin(), DA.end(), std::back_inserter(Arches)); 2322 } 2323 } 2324 } 2325 2326 std::string FnName = "isTarget", Test = "("; 2327 for (auto I = Arches.begin(), E = Arches.end(); I != E; ++I) { 2328 std::string Part = *I; 2329 Test += "Arch == llvm::Triple::" + Part; 2330 if (I + 1 != E) 2331 Test += " || "; 2332 FnName += Part; 2333 } 2334 Test += ")"; 2335 2336 // If the target also requires OS testing, generate those tests as well. 2337 bool UsesOS = false; 2338 if (!R->isValueUnset("OSes")) { 2339 UsesOS = true; 2340 2341 // We know that there was at least one arch test, so we need to and in the 2342 // OS tests. 2343 Test += " && ("; 2344 std::vector<std::string> OSes = R->getValueAsListOfStrings("OSes"); 2345 for (auto I = OSes.begin(), E = OSes.end(); I != E; ++I) { 2346 std::string Part = *I; 2347 2348 Test += "OS == llvm::Triple::" + Part; 2349 if (I + 1 != E) 2350 Test += " || "; 2351 FnName += Part; 2352 } 2353 Test += ")"; 2354 } 2355 2356 // If this code has already been generated, simply return the previous 2357 // instance of it. 2358 static std::set<std::string> CustomTargetSet; 2359 std::set<std::string>::iterator I = CustomTargetSet.find(FnName); 2360 if (I != CustomTargetSet.end()) 2361 return *I; 2362 2363 OS << "static bool " << FnName << "(const llvm::Triple &T) {\n"; 2364 OS << " llvm::Triple::ArchType Arch = T.getArch();\n"; 2365 if (UsesOS) 2366 OS << " llvm::Triple::OSType OS = T.getOS();\n"; 2367 OS << " return " << Test << ";\n"; 2368 OS << "}\n\n"; 2369 2370 CustomTargetSet.insert(FnName); 2371 return FnName; 2372 } 2373 2374 static void GenerateDefaultSpellingIndexToSemanticSpelling(raw_ostream &OS) { 2375 OS << "static unsigned defaultSpellingIndexToSemanticSpelling(" 2376 << "const AttributeList &Attr) {\n"; 2377 OS << " return UINT_MAX;\n"; 2378 OS << "}\n\n"; 2379 } 2380 2381 static std::string GenerateSpellingIndexToSemanticSpelling(const Record &Attr, 2382 raw_ostream &OS) { 2383 // If the attribute does not have a semantic form, we can bail out early. 2384 if (!Attr.getValueAsBit("ASTNode")) 2385 return "defaultSpellingIndexToSemanticSpelling"; 2386 2387 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(Attr); 2388 2389 // If there are zero or one spellings, or all of the spellings share the same 2390 // name, we can also bail out early. 2391 if (Spellings.size() <= 1 || SpellingNamesAreCommon(Spellings)) 2392 return "defaultSpellingIndexToSemanticSpelling"; 2393 2394 // Generate the enumeration we will use for the mapping. 2395 SemanticSpellingMap SemanticToSyntacticMap; 2396 std::string Enum = CreateSemanticSpellings(Spellings, SemanticToSyntacticMap); 2397 std::string Name = Attr.getName() + "AttrSpellingMap"; 2398 2399 OS << "static unsigned " << Name << "(const AttributeList &Attr) {\n"; 2400 OS << Enum; 2401 OS << " unsigned Idx = Attr.getAttributeSpellingListIndex();\n"; 2402 WriteSemanticSpellingSwitch("Idx", SemanticToSyntacticMap, OS); 2403 OS << "}\n\n"; 2404 2405 return Name; 2406 } 2407 2408 static bool IsKnownToGCC(const Record &Attr) { 2409 // Look at the spellings for this subject; if there are any spellings which 2410 // claim to be known to GCC, the attribute is known to GCC. 2411 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(Attr); 2412 for (const auto &I : Spellings) { 2413 if (I.knownToGCC()) 2414 return true; 2415 } 2416 return false; 2417 } 2418 2419 /// Emits the parsed attribute helpers 2420 void EmitClangAttrParsedAttrImpl(RecordKeeper &Records, raw_ostream &OS) { 2421 emitSourceFileHeader("Parsed attribute helpers", OS); 2422 2423 // Get the list of parsed attributes, and accept the optional list of 2424 // duplicates due to the ParseKind. 2425 ParsedAttrMap Dupes; 2426 ParsedAttrMap Attrs = getParsedAttrList(Records, &Dupes); 2427 2428 // Generate the default appertainsTo, target and language option diagnostic, 2429 // and spelling list index mapping methods. 2430 GenerateDefaultAppertainsTo(OS); 2431 GenerateDefaultLangOptRequirements(OS); 2432 GenerateDefaultTargetRequirements(OS); 2433 GenerateDefaultSpellingIndexToSemanticSpelling(OS); 2434 2435 // Generate the appertainsTo diagnostic methods and write their names into 2436 // another mapping. At the same time, generate the AttrInfoMap object 2437 // contents. Due to the reliance on generated code, use separate streams so 2438 // that code will not be interleaved. 2439 std::stringstream SS; 2440 for (auto I = Attrs.begin(), E = Attrs.end(); I != E; ++I) { 2441 // TODO: If the attribute's kind appears in the list of duplicates, that is 2442 // because it is a target-specific attribute that appears multiple times. 2443 // It would be beneficial to test whether the duplicates are "similar 2444 // enough" to each other to not cause problems. For instance, check that 2445 // the spellings are identical, and custom parsing rules match, etc. 2446 2447 // We need to generate struct instances based off ParsedAttrInfo from 2448 // AttributeList.cpp. 2449 SS << " { "; 2450 emitArgInfo(*I->second, SS); 2451 SS << ", " << I->second->getValueAsBit("HasCustomParsing"); 2452 SS << ", " << I->second->isSubClassOf("TargetSpecificAttr"); 2453 SS << ", " << I->second->isSubClassOf("TypeAttr"); 2454 SS << ", " << IsKnownToGCC(*I->second); 2455 SS << ", " << GenerateAppertainsTo(*I->second, OS); 2456 SS << ", " << GenerateLangOptRequirements(*I->second, OS); 2457 SS << ", " << GenerateTargetRequirements(*I->second, Dupes, OS); 2458 SS << ", " << GenerateSpellingIndexToSemanticSpelling(*I->second, OS); 2459 SS << " }"; 2460 2461 if (I + 1 != E) 2462 SS << ","; 2463 2464 SS << " // AT_" << I->first << "\n"; 2465 } 2466 2467 OS << "static const ParsedAttrInfo AttrInfoMap[AttributeList::UnknownAttribute + 1] = {\n"; 2468 OS << SS.str(); 2469 OS << "};\n\n"; 2470 } 2471 2472 // Emits the kind list of parsed attributes 2473 void EmitClangAttrParsedAttrKinds(RecordKeeper &Records, raw_ostream &OS) { 2474 emitSourceFileHeader("Attribute name matcher", OS); 2475 2476 std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr"); 2477 std::vector<StringMatcher::StringPair> GNU, Declspec, CXX11, Keywords; 2478 std::set<std::string> Seen; 2479 for (const auto *A : Attrs) { 2480 const Record &Attr = *A; 2481 2482 bool SemaHandler = Attr.getValueAsBit("SemaHandler"); 2483 bool Ignored = Attr.getValueAsBit("Ignored"); 2484 if (SemaHandler || Ignored) { 2485 // Attribute spellings can be shared between target-specific attributes, 2486 // and can be shared between syntaxes for the same attribute. For 2487 // instance, an attribute can be spelled GNU<"interrupt"> for an ARM- 2488 // specific attribute, or MSP430-specific attribute. Additionally, an 2489 // attribute can be spelled GNU<"dllexport"> and Declspec<"dllexport"> 2490 // for the same semantic attribute. Ultimately, we need to map each of 2491 // these to a single AttributeList::Kind value, but the StringMatcher 2492 // class cannot handle duplicate match strings. So we generate a list of 2493 // string to match based on the syntax, and emit multiple string matchers 2494 // depending on the syntax used. 2495 std::string AttrName; 2496 if (Attr.isSubClassOf("TargetSpecificAttr") && 2497 !Attr.isValueUnset("ParseKind")) { 2498 AttrName = Attr.getValueAsString("ParseKind"); 2499 if (Seen.find(AttrName) != Seen.end()) 2500 continue; 2501 Seen.insert(AttrName); 2502 } else 2503 AttrName = NormalizeAttrName(StringRef(Attr.getName())).str(); 2504 2505 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(Attr); 2506 for (const auto &S : Spellings) { 2507 std::string RawSpelling = S.name(); 2508 std::vector<StringMatcher::StringPair> *Matches = nullptr; 2509 std::string Spelling, Variety = S.variety(); 2510 if (Variety == "CXX11") { 2511 Matches = &CXX11; 2512 Spelling += S.nameSpace(); 2513 Spelling += "::"; 2514 } else if (Variety == "GNU") 2515 Matches = &GNU; 2516 else if (Variety == "Declspec") 2517 Matches = &Declspec; 2518 else if (Variety == "Keyword") 2519 Matches = &Keywords; 2520 2521 assert(Matches && "Unsupported spelling variety found"); 2522 2523 Spelling += NormalizeAttrSpelling(RawSpelling); 2524 if (SemaHandler) 2525 Matches->push_back(StringMatcher::StringPair(Spelling, 2526 "return AttributeList::AT_" + AttrName + ";")); 2527 else 2528 Matches->push_back(StringMatcher::StringPair(Spelling, 2529 "return AttributeList::IgnoredAttribute;")); 2530 } 2531 } 2532 } 2533 2534 OS << "static AttributeList::Kind getAttrKind(StringRef Name, "; 2535 OS << "AttributeList::Syntax Syntax) {\n"; 2536 OS << " if (AttributeList::AS_GNU == Syntax) {\n"; 2537 StringMatcher("Name", GNU, OS).Emit(); 2538 OS << " } else if (AttributeList::AS_Declspec == Syntax) {\n"; 2539 StringMatcher("Name", Declspec, OS).Emit(); 2540 OS << " } else if (AttributeList::AS_CXX11 == Syntax) {\n"; 2541 StringMatcher("Name", CXX11, OS).Emit(); 2542 OS << " } else if (AttributeList::AS_Keyword == Syntax) {\n"; 2543 StringMatcher("Name", Keywords, OS).Emit(); 2544 OS << " }\n"; 2545 OS << " return AttributeList::UnknownAttribute;\n" 2546 << "}\n"; 2547 } 2548 2549 // Emits the code to dump an attribute. 2550 void EmitClangAttrDump(RecordKeeper &Records, raw_ostream &OS) { 2551 emitSourceFileHeader("Attribute dumper", OS); 2552 2553 OS << 2554 " switch (A->getKind()) {\n" 2555 " default:\n" 2556 " llvm_unreachable(\"Unknown attribute kind!\");\n" 2557 " break;\n"; 2558 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"), Args; 2559 for (const auto *Attr : Attrs) { 2560 const Record &R = *Attr; 2561 if (!R.getValueAsBit("ASTNode")) 2562 continue; 2563 OS << " case attr::" << R.getName() << ": {\n"; 2564 2565 // If the attribute has a semantically-meaningful name (which is determined 2566 // by whether there is a Spelling enumeration for it), then write out the 2567 // spelling used for the attribute. 2568 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R); 2569 if (Spellings.size() > 1 && !SpellingNamesAreCommon(Spellings)) 2570 OS << " OS << \" \" << A->getSpelling();\n"; 2571 2572 Args = R.getValueAsListOfDefs("Args"); 2573 if (!Args.empty()) { 2574 OS << " const " << R.getName() << "Attr *SA = cast<" << R.getName() 2575 << "Attr>(A);\n"; 2576 for (const auto *Arg : Args) 2577 createArgument(*Arg, R.getName())->writeDump(OS); 2578 2579 // Code for detecting the last child. 2580 OS << " bool OldMoreChildren = hasMoreChildren();\n"; 2581 OS << " bool MoreChildren;\n"; 2582 2583 for (auto AI = Args.begin(), AE = Args.end(); AI != AE; ++AI) { 2584 // More code for detecting the last child. 2585 OS << " MoreChildren = OldMoreChildren"; 2586 for (auto Next = AI + 1; Next != AE; ++Next) { 2587 OS << " || "; 2588 createArgument(**Next, R.getName())->writeHasChildren(OS); 2589 } 2590 OS << ";\n"; 2591 OS << " setMoreChildren(MoreChildren);\n"; 2592 2593 createArgument(**AI, R.getName())->writeDumpChildren(OS); 2594 } 2595 2596 // Reset the last child. 2597 OS << " setMoreChildren(OldMoreChildren);\n"; 2598 } 2599 OS << 2600 " break;\n" 2601 " }\n"; 2602 } 2603 OS << " }\n"; 2604 } 2605 2606 void EmitClangAttrParserStringSwitches(RecordKeeper &Records, 2607 raw_ostream &OS) { 2608 emitSourceFileHeader("Parser-related llvm::StringSwitch cases", OS); 2609 emitClangAttrArgContextList(Records, OS); 2610 emitClangAttrIdentifierArgList(Records, OS); 2611 emitClangAttrTypeArgList(Records, OS); 2612 emitClangAttrLateParsedList(Records, OS); 2613 } 2614 2615 class DocumentationData { 2616 public: 2617 const Record *Documentation; 2618 const Record *Attribute; 2619 2620 DocumentationData(const Record &Documentation, const Record &Attribute) 2621 : Documentation(&Documentation), Attribute(&Attribute) {} 2622 }; 2623 2624 static void WriteCategoryHeader(const Record *DocCategory, 2625 raw_ostream &OS) { 2626 const std::string &Name = DocCategory->getValueAsString("Name"); 2627 OS << Name << "\n" << std::string(Name.length(), '=') << "\n"; 2628 2629 // If there is content, print that as well. 2630 std::string ContentStr = DocCategory->getValueAsString("Content"); 2631 if (!ContentStr.empty()) { 2632 // Trim leading and trailing newlines and spaces. 2633 StringRef Content(ContentStr); 2634 while (Content.startswith("\r") || Content.startswith("\n") || 2635 Content.startswith(" ") || Content.startswith("\t")) 2636 Content = Content.substr(1); 2637 while (Content.endswith("\r") || Content.endswith("\n") || 2638 Content.endswith(" ") || Content.endswith("\t")) 2639 Content = Content.substr(0, Content.size() - 1); 2640 OS << Content; 2641 } 2642 OS << "\n\n"; 2643 } 2644 2645 enum SpellingKind { 2646 GNU = 1 << 0, 2647 CXX11 = 1 << 1, 2648 Declspec = 1 << 2, 2649 Keyword = 1 << 3 2650 }; 2651 2652 static void WriteDocumentation(const DocumentationData &Doc, 2653 raw_ostream &OS) { 2654 // FIXME: there is no way to have a per-spelling category for the attribute 2655 // documentation. This may not be a limiting factor since the spellings 2656 // should generally be consistently applied across the category. 2657 2658 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*Doc.Attribute); 2659 2660 // Determine the heading to be used for this attribute. 2661 std::string Heading = Doc.Documentation->getValueAsString("Heading"); 2662 bool CustomHeading = !Heading.empty(); 2663 if (Heading.empty()) { 2664 // If there's only one spelling, we can simply use that. 2665 if (Spellings.size() == 1) 2666 Heading = Spellings.begin()->name(); 2667 else { 2668 std::set<std::string> Uniques; 2669 for (auto I = Spellings.begin(), E = Spellings.end(); 2670 I != E && Uniques.size() <= 1; ++I) { 2671 std::string Spelling = NormalizeNameForSpellingComparison(I->name()); 2672 Uniques.insert(Spelling); 2673 } 2674 // If the semantic map has only one spelling, that is sufficient for our 2675 // needs. 2676 if (Uniques.size() == 1) 2677 Heading = *Uniques.begin(); 2678 } 2679 } 2680 2681 // If the heading is still empty, it is an error. 2682 if (Heading.empty()) 2683 PrintFatalError(Doc.Attribute->getLoc(), 2684 "This attribute requires a heading to be specified"); 2685 2686 // Gather a list of unique spellings; this is not the same as the semantic 2687 // spelling for the attribute. Variations in underscores and other non- 2688 // semantic characters are still acceptable. 2689 std::vector<std::string> Names; 2690 2691 unsigned SupportedSpellings = 0; 2692 for (const auto &I : Spellings) { 2693 SpellingKind Kind = StringSwitch<SpellingKind>(I.variety()) 2694 .Case("GNU", GNU) 2695 .Case("CXX11", CXX11) 2696 .Case("Declspec", Declspec) 2697 .Case("Keyword", Keyword); 2698 2699 // Mask in the supported spelling. 2700 SupportedSpellings |= Kind; 2701 2702 std::string Name; 2703 if (Kind == CXX11 && !I.nameSpace().empty()) 2704 Name = I.nameSpace() + "::"; 2705 Name += I.name(); 2706 2707 // If this name is the same as the heading, do not add it. 2708 if (Name != Heading) 2709 Names.push_back(Name); 2710 } 2711 2712 // Print out the heading for the attribute. If there are alternate spellings, 2713 // then display those after the heading. 2714 if (!CustomHeading && !Names.empty()) { 2715 Heading += " ("; 2716 for (auto I = Names.begin(), E = Names.end(); I != E; ++I) { 2717 if (I != Names.begin()) 2718 Heading += ", "; 2719 Heading += *I; 2720 } 2721 Heading += ")"; 2722 } 2723 OS << Heading << "\n" << std::string(Heading.length(), '-') << "\n"; 2724 2725 if (!SupportedSpellings) 2726 PrintFatalError(Doc.Attribute->getLoc(), 2727 "Attribute has no supported spellings; cannot be " 2728 "documented"); 2729 2730 // List what spelling syntaxes the attribute supports. 2731 OS << ".. csv-table:: Supported Syntaxes\n"; 2732 OS << " :header: \"GNU\", \"C++11\", \"__declspec\", \"Keyword\"\n\n"; 2733 OS << " \""; 2734 if (SupportedSpellings & GNU) OS << "X"; 2735 OS << "\",\""; 2736 if (SupportedSpellings & CXX11) OS << "X"; 2737 OS << "\",\""; 2738 if (SupportedSpellings & Declspec) OS << "X"; 2739 OS << "\",\""; 2740 if (SupportedSpellings & Keyword) OS << "X"; 2741 OS << "\"\n\n"; 2742 2743 // If the attribute is deprecated, print a message about it, and possibly 2744 // provide a replacement attribute. 2745 if (!Doc.Documentation->isValueUnset("Deprecated")) { 2746 OS << "This attribute has been deprecated, and may be removed in a future " 2747 << "version of Clang."; 2748 const Record &Deprecated = *Doc.Documentation->getValueAsDef("Deprecated"); 2749 std::string Replacement = Deprecated.getValueAsString("Replacement"); 2750 if (!Replacement.empty()) 2751 OS << " This attribute has been superseded by ``" 2752 << Replacement << "``."; 2753 OS << "\n\n"; 2754 } 2755 2756 std::string ContentStr = Doc.Documentation->getValueAsString("Content"); 2757 // Trim leading and trailing newlines and spaces. 2758 StringRef Content(ContentStr); 2759 while (Content.startswith("\r") || Content.startswith("\n") || 2760 Content.startswith(" ") || Content.startswith("\t")) 2761 Content = Content.substr(1); 2762 while (Content.endswith("\r") || Content.endswith("\n") || 2763 Content.endswith(" ") || Content.endswith("\t")) 2764 Content = Content.substr(0, Content.size() - 1); 2765 OS << Content; 2766 2767 OS << "\n\n\n"; 2768 } 2769 2770 void EmitClangAttrDocs(RecordKeeper &Records, raw_ostream &OS) { 2771 // Get the documentation introduction paragraph. 2772 const Record *Documentation = Records.getDef("GlobalDocumentation"); 2773 if (!Documentation) { 2774 PrintFatalError("The Documentation top-level definition is missing, " 2775 "no documentation will be generated."); 2776 return; 2777 } 2778 2779 OS << Documentation->getValueAsString("Intro") << "\n"; 2780 2781 // Gather the Documentation lists from each of the attributes, based on the 2782 // category provided. 2783 std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr"); 2784 std::map<const Record *, std::vector<DocumentationData>> SplitDocs; 2785 for (const auto *A : Attrs) { 2786 const Record &Attr = *A; 2787 std::vector<Record *> Docs = Attr.getValueAsListOfDefs("Documentation"); 2788 for (const auto *D : Docs) { 2789 const Record &Doc = *D; 2790 const Record *Category = Doc.getValueAsDef("Category"); 2791 // If the category is "undocumented", then there cannot be any other 2792 // documentation categories (otherwise, the attribute would become 2793 // documented). 2794 std::string Cat = Category->getValueAsString("Name"); 2795 bool Undocumented = Cat == "Undocumented"; 2796 if (Undocumented && Docs.size() > 1) 2797 PrintFatalError(Doc.getLoc(), 2798 "Attribute is \"Undocumented\", but has multiple " 2799 "documentation categories"); 2800 2801 if (!Undocumented) 2802 SplitDocs[Category].push_back(DocumentationData(Doc, Attr)); 2803 } 2804 } 2805 2806 // Having split the attributes out based on what documentation goes where, 2807 // we can begin to generate sections of documentation. 2808 for (const auto &I : SplitDocs) { 2809 WriteCategoryHeader(I.first, OS); 2810 2811 // Walk over each of the attributes in the category and write out their 2812 // documentation. 2813 for (const auto &Doc : I.second) 2814 WriteDocumentation(Doc, OS); 2815 } 2816 } 2817 2818 } // end namespace clang 2819