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