1 //===--- CommentSema.cpp - Doxygen comment semantic analysis --------------===// 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 #include "clang/AST/CommentSema.h" 11 #include "clang/AST/Attr.h" 12 #include "clang/AST/CommentCommandTraits.h" 13 #include "clang/AST/CommentDiagnostic.h" 14 #include "clang/AST/Decl.h" 15 #include "clang/AST/DeclTemplate.h" 16 #include "clang/Basic/SourceManager.h" 17 #include "clang/Lex/Preprocessor.h" 18 #include "llvm/ADT/SmallString.h" 19 #include "llvm/ADT/StringSwitch.h" 20 21 namespace clang { 22 namespace comments { 23 24 namespace { 25 #include "clang/AST/CommentHTMLTagsProperties.inc" 26 } // unnamed namespace 27 28 Sema::Sema(llvm::BumpPtrAllocator &Allocator, const SourceManager &SourceMgr, 29 DiagnosticsEngine &Diags, CommandTraits &Traits, 30 const Preprocessor *PP) : 31 Allocator(Allocator), SourceMgr(SourceMgr), Diags(Diags), Traits(Traits), 32 PP(PP), ThisDeclInfo(NULL), BriefCommand(NULL), HeaderfileCommand(NULL) { 33 } 34 35 void Sema::setDecl(const Decl *D) { 36 if (!D) 37 return; 38 39 ThisDeclInfo = new (Allocator) DeclInfo; 40 ThisDeclInfo->CommentDecl = D; 41 ThisDeclInfo->IsFilled = false; 42 } 43 44 ParagraphComment *Sema::actOnParagraphComment( 45 ArrayRef<InlineContentComment *> Content) { 46 return new (Allocator) ParagraphComment(Content); 47 } 48 49 BlockCommandComment *Sema::actOnBlockCommandStart( 50 SourceLocation LocBegin, 51 SourceLocation LocEnd, 52 unsigned CommandID, 53 CommandMarkerKind CommandMarker) { 54 BlockCommandComment *BC = new (Allocator) BlockCommandComment(LocBegin, LocEnd, 55 CommandID, 56 CommandMarker); 57 checkContainerDecl(BC); 58 return BC; 59 } 60 61 void Sema::actOnBlockCommandArgs(BlockCommandComment *Command, 62 ArrayRef<BlockCommandComment::Argument> Args) { 63 Command->setArgs(Args); 64 } 65 66 void Sema::actOnBlockCommandFinish(BlockCommandComment *Command, 67 ParagraphComment *Paragraph) { 68 Command->setParagraph(Paragraph); 69 checkBlockCommandEmptyParagraph(Command); 70 checkBlockCommandDuplicate(Command); 71 if (ThisDeclInfo) { 72 // These checks only make sense if the comment is attached to a 73 // declaration. 74 checkReturnsCommand(Command); 75 checkDeprecatedCommand(Command); 76 } 77 } 78 79 ParamCommandComment *Sema::actOnParamCommandStart( 80 SourceLocation LocBegin, 81 SourceLocation LocEnd, 82 unsigned CommandID, 83 CommandMarkerKind CommandMarker) { 84 ParamCommandComment *Command = 85 new (Allocator) ParamCommandComment(LocBegin, LocEnd, CommandID, 86 CommandMarker); 87 88 if (!isFunctionDecl()) 89 Diag(Command->getLocation(), 90 diag::warn_doc_param_not_attached_to_a_function_decl) 91 << CommandMarker 92 << Command->getCommandNameRange(Traits); 93 94 return Command; 95 } 96 97 void Sema::checkFunctionDeclVerbatimLine(const BlockCommandComment *Comment) { 98 const CommandInfo *Info = Traits.getCommandInfo(Comment->getCommandID()); 99 if (!Info->IsFunctionDeclarationCommand) 100 return; 101 102 unsigned DiagSelect; 103 switch (Comment->getCommandID()) { 104 case CommandTraits::KCI_function: 105 DiagSelect = (!isAnyFunctionDecl() && !isFunctionTemplateDecl())? 1 : 0; 106 break; 107 case CommandTraits::KCI_functiongroup: 108 DiagSelect = (!isAnyFunctionDecl() && !isFunctionTemplateDecl())? 2 : 0; 109 break; 110 case CommandTraits::KCI_method: 111 DiagSelect = !isObjCMethodDecl() ? 3 : 0; 112 break; 113 case CommandTraits::KCI_methodgroup: 114 DiagSelect = !isObjCMethodDecl() ? 4 : 0; 115 break; 116 case CommandTraits::KCI_callback: 117 DiagSelect = !isFunctionPointerVarDecl() ? 5 : 0; 118 break; 119 default: 120 DiagSelect = 0; 121 break; 122 } 123 if (DiagSelect) 124 Diag(Comment->getLocation(), diag::warn_doc_function_method_decl_mismatch) 125 << Comment->getCommandMarker() 126 << (DiagSelect-1) << (DiagSelect-1) 127 << Comment->getSourceRange(); 128 } 129 130 void Sema::checkContainerDeclVerbatimLine(const BlockCommandComment *Comment) { 131 const CommandInfo *Info = Traits.getCommandInfo(Comment->getCommandID()); 132 if (!Info->IsRecordLikeDeclarationCommand) 133 return; 134 unsigned DiagSelect; 135 switch (Comment->getCommandID()) { 136 case CommandTraits::KCI_class: 137 DiagSelect = (!isClassOrStructDecl() && !isClassTemplateDecl()) ? 1 : 0; 138 // Allow @class command on @interface declarations. 139 // FIXME. Currently, \class and @class are indistinguishable. So, 140 // \class is also allowed on an @interface declaration 141 if (DiagSelect && Comment->getCommandMarker() && isObjCInterfaceDecl()) 142 DiagSelect = 0; 143 break; 144 case CommandTraits::KCI_interface: 145 DiagSelect = !isObjCInterfaceDecl() ? 2 : 0; 146 break; 147 case CommandTraits::KCI_protocol: 148 DiagSelect = !isObjCProtocolDecl() ? 3 : 0; 149 break; 150 case CommandTraits::KCI_struct: 151 DiagSelect = !isClassOrStructDecl() ? 4 : 0; 152 break; 153 case CommandTraits::KCI_union: 154 DiagSelect = !isUnionDecl() ? 5 : 0; 155 break; 156 default: 157 DiagSelect = 0; 158 break; 159 } 160 if (DiagSelect) 161 Diag(Comment->getLocation(), diag::warn_doc_api_container_decl_mismatch) 162 << Comment->getCommandMarker() 163 << (DiagSelect-1) << (DiagSelect-1) 164 << Comment->getSourceRange(); 165 } 166 167 void Sema::checkContainerDecl(const BlockCommandComment *Comment) { 168 const CommandInfo *Info = Traits.getCommandInfo(Comment->getCommandID()); 169 if (!Info->IsRecordLikeDetailCommand || isRecordLikeDecl()) 170 return; 171 unsigned DiagSelect; 172 switch (Comment->getCommandID()) { 173 case CommandTraits::KCI_classdesign: 174 DiagSelect = 1; 175 break; 176 case CommandTraits::KCI_coclass: 177 DiagSelect = 2; 178 break; 179 case CommandTraits::KCI_dependency: 180 DiagSelect = 3; 181 break; 182 case CommandTraits::KCI_helper: 183 DiagSelect = 4; 184 break; 185 case CommandTraits::KCI_helperclass: 186 DiagSelect = 5; 187 break; 188 case CommandTraits::KCI_helps: 189 DiagSelect = 6; 190 break; 191 case CommandTraits::KCI_instancesize: 192 DiagSelect = 7; 193 break; 194 case CommandTraits::KCI_ownership: 195 DiagSelect = 8; 196 break; 197 case CommandTraits::KCI_performance: 198 DiagSelect = 9; 199 break; 200 case CommandTraits::KCI_security: 201 DiagSelect = 10; 202 break; 203 case CommandTraits::KCI_superclass: 204 DiagSelect = 11; 205 break; 206 default: 207 DiagSelect = 0; 208 break; 209 } 210 if (DiagSelect) 211 Diag(Comment->getLocation(), diag::warn_doc_container_decl_mismatch) 212 << Comment->getCommandMarker() 213 << (DiagSelect-1) 214 << Comment->getSourceRange(); 215 } 216 217 /// \brief Turn a string into the corresponding PassDirection or -1 if it's not 218 /// valid. 219 static int getParamPassDirection(StringRef Arg) { 220 return llvm::StringSwitch<int>(Arg) 221 .Case("[in]", ParamCommandComment::In) 222 .Case("[out]", ParamCommandComment::Out) 223 .Cases("[in,out]", "[out,in]", ParamCommandComment::InOut) 224 .Default(-1); 225 } 226 227 void Sema::actOnParamCommandDirectionArg(ParamCommandComment *Command, 228 SourceLocation ArgLocBegin, 229 SourceLocation ArgLocEnd, 230 StringRef Arg) { 231 std::string ArgLower = Arg.lower(); 232 int Direction = getParamPassDirection(ArgLower); 233 234 if (Direction == -1) { 235 // Try again with whitespace removed. 236 ArgLower.erase( 237 std::remove_if(ArgLower.begin(), ArgLower.end(), clang::isWhitespace), 238 ArgLower.end()); 239 Direction = getParamPassDirection(ArgLower); 240 241 SourceRange ArgRange(ArgLocBegin, ArgLocEnd); 242 if (Direction != -1) { 243 const char *FixedName = ParamCommandComment::getDirectionAsString( 244 (ParamCommandComment::PassDirection)Direction); 245 Diag(ArgLocBegin, diag::warn_doc_param_spaces_in_direction) 246 << ArgRange << FixItHint::CreateReplacement(ArgRange, FixedName); 247 } else { 248 Diag(ArgLocBegin, diag::warn_doc_param_invalid_direction) << ArgRange; 249 Direction = ParamCommandComment::In; // Sane fall back. 250 } 251 } 252 Command->setDirection((ParamCommandComment::PassDirection)Direction, 253 /*Explicit=*/true); 254 } 255 256 void Sema::actOnParamCommandParamNameArg(ParamCommandComment *Command, 257 SourceLocation ArgLocBegin, 258 SourceLocation ArgLocEnd, 259 StringRef Arg) { 260 // Parser will not feed us more arguments than needed. 261 assert(Command->getNumArgs() == 0); 262 263 if (!Command->isDirectionExplicit()) { 264 // User didn't provide a direction argument. 265 Command->setDirection(ParamCommandComment::In, /* Explicit = */ false); 266 } 267 typedef BlockCommandComment::Argument Argument; 268 Argument *A = new (Allocator) Argument(SourceRange(ArgLocBegin, 269 ArgLocEnd), 270 Arg); 271 Command->setArgs(llvm::makeArrayRef(A, 1)); 272 } 273 274 void Sema::actOnParamCommandFinish(ParamCommandComment *Command, 275 ParagraphComment *Paragraph) { 276 Command->setParagraph(Paragraph); 277 checkBlockCommandEmptyParagraph(Command); 278 } 279 280 TParamCommandComment *Sema::actOnTParamCommandStart( 281 SourceLocation LocBegin, 282 SourceLocation LocEnd, 283 unsigned CommandID, 284 CommandMarkerKind CommandMarker) { 285 TParamCommandComment *Command = 286 new (Allocator) TParamCommandComment(LocBegin, LocEnd, CommandID, 287 CommandMarker); 288 289 if (!isTemplateOrSpecialization()) 290 Diag(Command->getLocation(), 291 diag::warn_doc_tparam_not_attached_to_a_template_decl) 292 << CommandMarker 293 << Command->getCommandNameRange(Traits); 294 295 return Command; 296 } 297 298 void Sema::actOnTParamCommandParamNameArg(TParamCommandComment *Command, 299 SourceLocation ArgLocBegin, 300 SourceLocation ArgLocEnd, 301 StringRef Arg) { 302 // Parser will not feed us more arguments than needed. 303 assert(Command->getNumArgs() == 0); 304 305 typedef BlockCommandComment::Argument Argument; 306 Argument *A = new (Allocator) Argument(SourceRange(ArgLocBegin, 307 ArgLocEnd), 308 Arg); 309 Command->setArgs(llvm::makeArrayRef(A, 1)); 310 311 if (!isTemplateOrSpecialization()) { 312 // We already warned that this \\tparam is not attached to a template decl. 313 return; 314 } 315 316 const TemplateParameterList *TemplateParameters = 317 ThisDeclInfo->TemplateParameters; 318 SmallVector<unsigned, 2> Position; 319 if (resolveTParamReference(Arg, TemplateParameters, &Position)) { 320 Command->setPosition(copyArray(llvm::makeArrayRef(Position))); 321 TParamCommandComment *&PrevCommand = TemplateParameterDocs[Arg]; 322 if (PrevCommand) { 323 SourceRange ArgRange(ArgLocBegin, ArgLocEnd); 324 Diag(ArgLocBegin, diag::warn_doc_tparam_duplicate) 325 << Arg << ArgRange; 326 Diag(PrevCommand->getLocation(), diag::note_doc_tparam_previous) 327 << PrevCommand->getParamNameRange(); 328 } 329 PrevCommand = Command; 330 return; 331 } 332 333 SourceRange ArgRange(ArgLocBegin, ArgLocEnd); 334 Diag(ArgLocBegin, diag::warn_doc_tparam_not_found) 335 << Arg << ArgRange; 336 337 if (!TemplateParameters || TemplateParameters->size() == 0) 338 return; 339 340 StringRef CorrectedName; 341 if (TemplateParameters->size() == 1) { 342 const NamedDecl *Param = TemplateParameters->getParam(0); 343 const IdentifierInfo *II = Param->getIdentifier(); 344 if (II) 345 CorrectedName = II->getName(); 346 } else { 347 CorrectedName = correctTypoInTParamReference(Arg, TemplateParameters); 348 } 349 350 if (!CorrectedName.empty()) { 351 Diag(ArgLocBegin, diag::note_doc_tparam_name_suggestion) 352 << CorrectedName 353 << FixItHint::CreateReplacement(ArgRange, CorrectedName); 354 } 355 356 return; 357 } 358 359 void Sema::actOnTParamCommandFinish(TParamCommandComment *Command, 360 ParagraphComment *Paragraph) { 361 Command->setParagraph(Paragraph); 362 checkBlockCommandEmptyParagraph(Command); 363 } 364 365 InlineCommandComment *Sema::actOnInlineCommand(SourceLocation CommandLocBegin, 366 SourceLocation CommandLocEnd, 367 unsigned CommandID) { 368 ArrayRef<InlineCommandComment::Argument> Args; 369 StringRef CommandName = Traits.getCommandInfo(CommandID)->Name; 370 return new (Allocator) InlineCommandComment( 371 CommandLocBegin, 372 CommandLocEnd, 373 CommandID, 374 getInlineCommandRenderKind(CommandName), 375 Args); 376 } 377 378 InlineCommandComment *Sema::actOnInlineCommand(SourceLocation CommandLocBegin, 379 SourceLocation CommandLocEnd, 380 unsigned CommandID, 381 SourceLocation ArgLocBegin, 382 SourceLocation ArgLocEnd, 383 StringRef Arg) { 384 typedef InlineCommandComment::Argument Argument; 385 Argument *A = new (Allocator) Argument(SourceRange(ArgLocBegin, 386 ArgLocEnd), 387 Arg); 388 StringRef CommandName = Traits.getCommandInfo(CommandID)->Name; 389 390 return new (Allocator) InlineCommandComment( 391 CommandLocBegin, 392 CommandLocEnd, 393 CommandID, 394 getInlineCommandRenderKind(CommandName), 395 llvm::makeArrayRef(A, 1)); 396 } 397 398 InlineContentComment *Sema::actOnUnknownCommand(SourceLocation LocBegin, 399 SourceLocation LocEnd, 400 StringRef CommandName) { 401 unsigned CommandID = Traits.registerUnknownCommand(CommandName)->getID(); 402 return actOnUnknownCommand(LocBegin, LocEnd, CommandID); 403 } 404 405 InlineContentComment *Sema::actOnUnknownCommand(SourceLocation LocBegin, 406 SourceLocation LocEnd, 407 unsigned CommandID) { 408 ArrayRef<InlineCommandComment::Argument> Args; 409 return new (Allocator) InlineCommandComment( 410 LocBegin, LocEnd, CommandID, 411 InlineCommandComment::RenderNormal, 412 Args); 413 } 414 415 TextComment *Sema::actOnText(SourceLocation LocBegin, 416 SourceLocation LocEnd, 417 StringRef Text) { 418 return new (Allocator) TextComment(LocBegin, LocEnd, Text); 419 } 420 421 VerbatimBlockComment *Sema::actOnVerbatimBlockStart(SourceLocation Loc, 422 unsigned CommandID) { 423 StringRef CommandName = Traits.getCommandInfo(CommandID)->Name; 424 return new (Allocator) VerbatimBlockComment( 425 Loc, 426 Loc.getLocWithOffset(1 + CommandName.size()), 427 CommandID); 428 } 429 430 VerbatimBlockLineComment *Sema::actOnVerbatimBlockLine(SourceLocation Loc, 431 StringRef Text) { 432 return new (Allocator) VerbatimBlockLineComment(Loc, Text); 433 } 434 435 void Sema::actOnVerbatimBlockFinish( 436 VerbatimBlockComment *Block, 437 SourceLocation CloseNameLocBegin, 438 StringRef CloseName, 439 ArrayRef<VerbatimBlockLineComment *> Lines) { 440 Block->setCloseName(CloseName, CloseNameLocBegin); 441 Block->setLines(Lines); 442 } 443 444 VerbatimLineComment *Sema::actOnVerbatimLine(SourceLocation LocBegin, 445 unsigned CommandID, 446 SourceLocation TextBegin, 447 StringRef Text) { 448 VerbatimLineComment *VL = new (Allocator) VerbatimLineComment( 449 LocBegin, 450 TextBegin.getLocWithOffset(Text.size()), 451 CommandID, 452 TextBegin, 453 Text); 454 checkFunctionDeclVerbatimLine(VL); 455 checkContainerDeclVerbatimLine(VL); 456 return VL; 457 } 458 459 HTMLStartTagComment *Sema::actOnHTMLStartTagStart(SourceLocation LocBegin, 460 StringRef TagName) { 461 return new (Allocator) HTMLStartTagComment(LocBegin, TagName); 462 } 463 464 void Sema::actOnHTMLStartTagFinish( 465 HTMLStartTagComment *Tag, 466 ArrayRef<HTMLStartTagComment::Attribute> Attrs, 467 SourceLocation GreaterLoc, 468 bool IsSelfClosing) { 469 Tag->setAttrs(Attrs); 470 Tag->setGreaterLoc(GreaterLoc); 471 if (IsSelfClosing) 472 Tag->setSelfClosing(); 473 else if (!isHTMLEndTagForbidden(Tag->getTagName())) 474 HTMLOpenTags.push_back(Tag); 475 } 476 477 HTMLEndTagComment *Sema::actOnHTMLEndTag(SourceLocation LocBegin, 478 SourceLocation LocEnd, 479 StringRef TagName) { 480 HTMLEndTagComment *HET = 481 new (Allocator) HTMLEndTagComment(LocBegin, LocEnd, TagName); 482 if (isHTMLEndTagForbidden(TagName)) { 483 Diag(HET->getLocation(), diag::warn_doc_html_end_forbidden) 484 << TagName << HET->getSourceRange(); 485 return HET; 486 } 487 488 bool FoundOpen = false; 489 for (SmallVectorImpl<HTMLStartTagComment *>::const_reverse_iterator 490 I = HTMLOpenTags.rbegin(), E = HTMLOpenTags.rend(); 491 I != E; ++I) { 492 if ((*I)->getTagName() == TagName) { 493 FoundOpen = true; 494 break; 495 } 496 } 497 if (!FoundOpen) { 498 Diag(HET->getLocation(), diag::warn_doc_html_end_unbalanced) 499 << HET->getSourceRange(); 500 return HET; 501 } 502 503 while (!HTMLOpenTags.empty()) { 504 const HTMLStartTagComment *HST = HTMLOpenTags.pop_back_val(); 505 StringRef LastNotClosedTagName = HST->getTagName(); 506 if (LastNotClosedTagName == TagName) 507 break; 508 509 if (isHTMLEndTagOptional(LastNotClosedTagName)) 510 continue; 511 512 bool OpenLineInvalid; 513 const unsigned OpenLine = SourceMgr.getPresumedLineNumber( 514 HST->getLocation(), 515 &OpenLineInvalid); 516 bool CloseLineInvalid; 517 const unsigned CloseLine = SourceMgr.getPresumedLineNumber( 518 HET->getLocation(), 519 &CloseLineInvalid); 520 521 if (OpenLineInvalid || CloseLineInvalid || OpenLine == CloseLine) 522 Diag(HST->getLocation(), diag::warn_doc_html_start_end_mismatch) 523 << HST->getTagName() << HET->getTagName() 524 << HST->getSourceRange() << HET->getSourceRange(); 525 else { 526 Diag(HST->getLocation(), diag::warn_doc_html_start_end_mismatch) 527 << HST->getTagName() << HET->getTagName() 528 << HST->getSourceRange(); 529 Diag(HET->getLocation(), diag::note_doc_html_end_tag) 530 << HET->getSourceRange(); 531 } 532 } 533 534 return HET; 535 } 536 537 FullComment *Sema::actOnFullComment( 538 ArrayRef<BlockContentComment *> Blocks) { 539 FullComment *FC = new (Allocator) FullComment(Blocks, ThisDeclInfo); 540 resolveParamCommandIndexes(FC); 541 return FC; 542 } 543 544 void Sema::checkBlockCommandEmptyParagraph(BlockCommandComment *Command) { 545 if (Traits.getCommandInfo(Command->getCommandID())->IsEmptyParagraphAllowed) 546 return; 547 548 ParagraphComment *Paragraph = Command->getParagraph(); 549 if (Paragraph->isWhitespace()) { 550 SourceLocation DiagLoc; 551 if (Command->getNumArgs() > 0) 552 DiagLoc = Command->getArgRange(Command->getNumArgs() - 1).getEnd(); 553 if (!DiagLoc.isValid()) 554 DiagLoc = Command->getCommandNameRange(Traits).getEnd(); 555 Diag(DiagLoc, diag::warn_doc_block_command_empty_paragraph) 556 << Command->getCommandMarker() 557 << Command->getCommandName(Traits) 558 << Command->getSourceRange(); 559 } 560 } 561 562 void Sema::checkReturnsCommand(const BlockCommandComment *Command) { 563 if (!Traits.getCommandInfo(Command->getCommandID())->IsReturnsCommand) 564 return; 565 566 assert(ThisDeclInfo && "should not call this check on a bare comment"); 567 568 if (isFunctionDecl()) { 569 if (ThisDeclInfo->ReturnType->isVoidType()) { 570 unsigned DiagKind; 571 switch (ThisDeclInfo->CommentDecl->getKind()) { 572 default: 573 if (ThisDeclInfo->IsObjCMethod) 574 DiagKind = 3; 575 else 576 DiagKind = 0; 577 break; 578 case Decl::CXXConstructor: 579 DiagKind = 1; 580 break; 581 case Decl::CXXDestructor: 582 DiagKind = 2; 583 break; 584 } 585 Diag(Command->getLocation(), 586 diag::warn_doc_returns_attached_to_a_void_function) 587 << Command->getCommandMarker() 588 << Command->getCommandName(Traits) 589 << DiagKind 590 << Command->getSourceRange(); 591 } 592 return; 593 } 594 else if (isObjCPropertyDecl()) 595 return; 596 597 Diag(Command->getLocation(), 598 diag::warn_doc_returns_not_attached_to_a_function_decl) 599 << Command->getCommandMarker() 600 << Command->getCommandName(Traits) 601 << Command->getSourceRange(); 602 } 603 604 void Sema::checkBlockCommandDuplicate(const BlockCommandComment *Command) { 605 const CommandInfo *Info = Traits.getCommandInfo(Command->getCommandID()); 606 const BlockCommandComment *PrevCommand = NULL; 607 if (Info->IsBriefCommand) { 608 if (!BriefCommand) { 609 BriefCommand = Command; 610 return; 611 } 612 PrevCommand = BriefCommand; 613 } else if (Info->IsHeaderfileCommand) { 614 if (!HeaderfileCommand) { 615 HeaderfileCommand = Command; 616 return; 617 } 618 PrevCommand = HeaderfileCommand; 619 } else { 620 // We don't want to check this command for duplicates. 621 return; 622 } 623 StringRef CommandName = Command->getCommandName(Traits); 624 StringRef PrevCommandName = PrevCommand->getCommandName(Traits); 625 Diag(Command->getLocation(), diag::warn_doc_block_command_duplicate) 626 << Command->getCommandMarker() 627 << CommandName 628 << Command->getSourceRange(); 629 if (CommandName == PrevCommandName) 630 Diag(PrevCommand->getLocation(), diag::note_doc_block_command_previous) 631 << PrevCommand->getCommandMarker() 632 << PrevCommandName 633 << PrevCommand->getSourceRange(); 634 else 635 Diag(PrevCommand->getLocation(), 636 diag::note_doc_block_command_previous_alias) 637 << PrevCommand->getCommandMarker() 638 << PrevCommandName 639 << CommandName; 640 } 641 642 void Sema::checkDeprecatedCommand(const BlockCommandComment *Command) { 643 if (!Traits.getCommandInfo(Command->getCommandID())->IsDeprecatedCommand) 644 return; 645 646 assert(ThisDeclInfo && "should not call this check on a bare comment"); 647 648 const Decl *D = ThisDeclInfo->CommentDecl; 649 if (!D) 650 return; 651 652 if (D->hasAttr<DeprecatedAttr>() || 653 D->hasAttr<AvailabilityAttr>() || 654 D->hasAttr<UnavailableAttr>()) 655 return; 656 657 Diag(Command->getLocation(), 658 diag::warn_doc_deprecated_not_sync) 659 << Command->getSourceRange(); 660 661 // Try to emit a fixit with a deprecation attribute. 662 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 663 // Don't emit a Fix-It for non-member function definitions. GCC does not 664 // accept attributes on them. 665 const DeclContext *Ctx = FD->getDeclContext(); 666 if ((!Ctx || !Ctx->isRecord()) && 667 FD->doesThisDeclarationHaveABody()) 668 return; 669 670 StringRef AttributeSpelling = "__attribute__((deprecated))"; 671 if (PP) { 672 TokenValue Tokens[] = { 673 tok::kw___attribute, tok::l_paren, tok::l_paren, 674 PP->getIdentifierInfo("deprecated"), 675 tok::r_paren, tok::r_paren 676 }; 677 StringRef MacroName = PP->getLastMacroWithSpelling(FD->getLocation(), 678 Tokens); 679 if (!MacroName.empty()) 680 AttributeSpelling = MacroName; 681 } 682 683 SmallString<64> TextToInsert(" "); 684 TextToInsert += AttributeSpelling; 685 Diag(FD->getLocEnd(), 686 diag::note_add_deprecation_attr) 687 << FixItHint::CreateInsertion(FD->getLocEnd().getLocWithOffset(1), 688 TextToInsert); 689 } 690 } 691 692 void Sema::resolveParamCommandIndexes(const FullComment *FC) { 693 if (!isFunctionDecl()) { 694 // We already warned that \\param commands are not attached to a function 695 // decl. 696 return; 697 } 698 699 SmallVector<ParamCommandComment *, 8> UnresolvedParamCommands; 700 701 // Comment AST nodes that correspond to \c ParamVars for which we have 702 // found a \\param command or NULL if no documentation was found so far. 703 SmallVector<ParamCommandComment *, 8> ParamVarDocs; 704 705 ArrayRef<const ParmVarDecl *> ParamVars = getParamVars(); 706 ParamVarDocs.resize(ParamVars.size(), NULL); 707 708 // First pass over all \\param commands: resolve all parameter names. 709 for (Comment::child_iterator I = FC->child_begin(), E = FC->child_end(); 710 I != E; ++I) { 711 ParamCommandComment *PCC = dyn_cast<ParamCommandComment>(*I); 712 if (!PCC || !PCC->hasParamName()) 713 continue; 714 StringRef ParamName = PCC->getParamNameAsWritten(); 715 716 // Check that referenced parameter name is in the function decl. 717 const unsigned ResolvedParamIndex = resolveParmVarReference(ParamName, 718 ParamVars); 719 if (ResolvedParamIndex == ParamCommandComment::VarArgParamIndex) { 720 PCC->setIsVarArgParam(); 721 continue; 722 } 723 if (ResolvedParamIndex == ParamCommandComment::InvalidParamIndex) { 724 UnresolvedParamCommands.push_back(PCC); 725 continue; 726 } 727 PCC->setParamIndex(ResolvedParamIndex); 728 if (ParamVarDocs[ResolvedParamIndex]) { 729 SourceRange ArgRange = PCC->getParamNameRange(); 730 Diag(ArgRange.getBegin(), diag::warn_doc_param_duplicate) 731 << ParamName << ArgRange; 732 ParamCommandComment *PrevCommand = ParamVarDocs[ResolvedParamIndex]; 733 Diag(PrevCommand->getLocation(), diag::note_doc_param_previous) 734 << PrevCommand->getParamNameRange(); 735 } 736 ParamVarDocs[ResolvedParamIndex] = PCC; 737 } 738 739 // Find parameter declarations that have no corresponding \\param. 740 SmallVector<const ParmVarDecl *, 8> OrphanedParamDecls; 741 for (unsigned i = 0, e = ParamVarDocs.size(); i != e; ++i) { 742 if (!ParamVarDocs[i]) 743 OrphanedParamDecls.push_back(ParamVars[i]); 744 } 745 746 // Second pass over unresolved \\param commands: do typo correction. 747 // Suggest corrections from a set of parameter declarations that have no 748 // corresponding \\param. 749 for (unsigned i = 0, e = UnresolvedParamCommands.size(); i != e; ++i) { 750 const ParamCommandComment *PCC = UnresolvedParamCommands[i]; 751 752 SourceRange ArgRange = PCC->getParamNameRange(); 753 StringRef ParamName = PCC->getParamNameAsWritten(); 754 Diag(ArgRange.getBegin(), diag::warn_doc_param_not_found) 755 << ParamName << ArgRange; 756 757 // All parameters documented -- can't suggest a correction. 758 if (OrphanedParamDecls.size() == 0) 759 continue; 760 761 unsigned CorrectedParamIndex = ParamCommandComment::InvalidParamIndex; 762 if (OrphanedParamDecls.size() == 1) { 763 // If one parameter is not documented then that parameter is the only 764 // possible suggestion. 765 CorrectedParamIndex = 0; 766 } else { 767 // Do typo correction. 768 CorrectedParamIndex = correctTypoInParmVarReference(ParamName, 769 OrphanedParamDecls); 770 } 771 if (CorrectedParamIndex != ParamCommandComment::InvalidParamIndex) { 772 const ParmVarDecl *CorrectedPVD = OrphanedParamDecls[CorrectedParamIndex]; 773 if (const IdentifierInfo *CorrectedII = CorrectedPVD->getIdentifier()) 774 Diag(ArgRange.getBegin(), diag::note_doc_param_name_suggestion) 775 << CorrectedII->getName() 776 << FixItHint::CreateReplacement(ArgRange, CorrectedII->getName()); 777 } 778 } 779 } 780 781 bool Sema::isFunctionDecl() { 782 if (!ThisDeclInfo) 783 return false; 784 if (!ThisDeclInfo->IsFilled) 785 inspectThisDecl(); 786 return ThisDeclInfo->getKind() == DeclInfo::FunctionKind; 787 } 788 789 bool Sema::isAnyFunctionDecl() { 790 return isFunctionDecl() && ThisDeclInfo->CurrentDecl && 791 isa<FunctionDecl>(ThisDeclInfo->CurrentDecl); 792 } 793 794 bool Sema::isFunctionOrMethodVariadic() { 795 if (!isAnyFunctionDecl() && !isObjCMethodDecl()) 796 return false; 797 if (const FunctionDecl *FD = 798 dyn_cast<FunctionDecl>(ThisDeclInfo->CurrentDecl)) 799 return FD->isVariadic(); 800 if (const ObjCMethodDecl *MD = 801 dyn_cast<ObjCMethodDecl>(ThisDeclInfo->CurrentDecl)) 802 return MD->isVariadic(); 803 return false; 804 } 805 806 bool Sema::isObjCMethodDecl() { 807 return isFunctionDecl() && ThisDeclInfo->CurrentDecl && 808 isa<ObjCMethodDecl>(ThisDeclInfo->CurrentDecl); 809 } 810 811 bool Sema::isFunctionPointerVarDecl() { 812 if (!ThisDeclInfo) 813 return false; 814 if (!ThisDeclInfo->IsFilled) 815 inspectThisDecl(); 816 if (ThisDeclInfo->getKind() == DeclInfo::VariableKind) { 817 if (const VarDecl *VD = dyn_cast_or_null<VarDecl>(ThisDeclInfo->CurrentDecl)) { 818 QualType QT = VD->getType(); 819 return QT->isFunctionPointerType(); 820 } 821 } 822 return false; 823 } 824 825 bool Sema::isObjCPropertyDecl() { 826 if (!ThisDeclInfo) 827 return false; 828 if (!ThisDeclInfo->IsFilled) 829 inspectThisDecl(); 830 return ThisDeclInfo->CurrentDecl->getKind() == Decl::ObjCProperty; 831 } 832 833 bool Sema::isTemplateOrSpecialization() { 834 if (!ThisDeclInfo) 835 return false; 836 if (!ThisDeclInfo->IsFilled) 837 inspectThisDecl(); 838 return ThisDeclInfo->getTemplateKind() != DeclInfo::NotTemplate; 839 } 840 841 bool Sema::isRecordLikeDecl() { 842 if (!ThisDeclInfo) 843 return false; 844 if (!ThisDeclInfo->IsFilled) 845 inspectThisDecl(); 846 return isUnionDecl() || isClassOrStructDecl() || isObjCInterfaceDecl() || 847 isObjCProtocolDecl(); 848 } 849 850 bool Sema::isUnionDecl() { 851 if (!ThisDeclInfo) 852 return false; 853 if (!ThisDeclInfo->IsFilled) 854 inspectThisDecl(); 855 if (const RecordDecl *RD = 856 dyn_cast_or_null<RecordDecl>(ThisDeclInfo->CurrentDecl)) 857 return RD->isUnion(); 858 return false; 859 } 860 861 bool Sema::isClassOrStructDecl() { 862 if (!ThisDeclInfo) 863 return false; 864 if (!ThisDeclInfo->IsFilled) 865 inspectThisDecl(); 866 return ThisDeclInfo->CurrentDecl && 867 isa<RecordDecl>(ThisDeclInfo->CurrentDecl) && 868 !isUnionDecl(); 869 } 870 871 bool Sema::isClassTemplateDecl() { 872 if (!ThisDeclInfo) 873 return false; 874 if (!ThisDeclInfo->IsFilled) 875 inspectThisDecl(); 876 return ThisDeclInfo->CurrentDecl && 877 (isa<ClassTemplateDecl>(ThisDeclInfo->CurrentDecl)); 878 } 879 880 bool Sema::isFunctionTemplateDecl() { 881 if (!ThisDeclInfo) 882 return false; 883 if (!ThisDeclInfo->IsFilled) 884 inspectThisDecl(); 885 return ThisDeclInfo->CurrentDecl && 886 (isa<FunctionTemplateDecl>(ThisDeclInfo->CurrentDecl)); 887 } 888 889 bool Sema::isObjCInterfaceDecl() { 890 if (!ThisDeclInfo) 891 return false; 892 if (!ThisDeclInfo->IsFilled) 893 inspectThisDecl(); 894 return ThisDeclInfo->CurrentDecl && 895 isa<ObjCInterfaceDecl>(ThisDeclInfo->CurrentDecl); 896 } 897 898 bool Sema::isObjCProtocolDecl() { 899 if (!ThisDeclInfo) 900 return false; 901 if (!ThisDeclInfo->IsFilled) 902 inspectThisDecl(); 903 return ThisDeclInfo->CurrentDecl && 904 isa<ObjCProtocolDecl>(ThisDeclInfo->CurrentDecl); 905 } 906 907 ArrayRef<const ParmVarDecl *> Sema::getParamVars() { 908 if (!ThisDeclInfo->IsFilled) 909 inspectThisDecl(); 910 return ThisDeclInfo->ParamVars; 911 } 912 913 void Sema::inspectThisDecl() { 914 ThisDeclInfo->fill(); 915 } 916 917 unsigned Sema::resolveParmVarReference(StringRef Name, 918 ArrayRef<const ParmVarDecl *> ParamVars) { 919 for (unsigned i = 0, e = ParamVars.size(); i != e; ++i) { 920 const IdentifierInfo *II = ParamVars[i]->getIdentifier(); 921 if (II && II->getName() == Name) 922 return i; 923 } 924 if (Name == "..." && isFunctionOrMethodVariadic()) 925 return ParamCommandComment::VarArgParamIndex; 926 return ParamCommandComment::InvalidParamIndex; 927 } 928 929 namespace { 930 class SimpleTypoCorrector { 931 StringRef Typo; 932 const unsigned MaxEditDistance; 933 934 const NamedDecl *BestDecl; 935 unsigned BestEditDistance; 936 unsigned BestIndex; 937 unsigned NextIndex; 938 939 public: 940 SimpleTypoCorrector(StringRef Typo) : 941 Typo(Typo), MaxEditDistance((Typo.size() + 2) / 3), 942 BestDecl(NULL), BestEditDistance(MaxEditDistance + 1), 943 BestIndex(0), NextIndex(0) 944 { } 945 946 void addDecl(const NamedDecl *ND); 947 948 const NamedDecl *getBestDecl() const { 949 if (BestEditDistance > MaxEditDistance) 950 return NULL; 951 952 return BestDecl; 953 } 954 955 unsigned getBestDeclIndex() const { 956 assert(getBestDecl()); 957 return BestIndex; 958 } 959 }; 960 961 void SimpleTypoCorrector::addDecl(const NamedDecl *ND) { 962 unsigned CurrIndex = NextIndex++; 963 964 const IdentifierInfo *II = ND->getIdentifier(); 965 if (!II) 966 return; 967 968 StringRef Name = II->getName(); 969 unsigned MinPossibleEditDistance = abs((int)Name.size() - (int)Typo.size()); 970 if (MinPossibleEditDistance > 0 && 971 Typo.size() / MinPossibleEditDistance < 3) 972 return; 973 974 unsigned EditDistance = Typo.edit_distance(Name, true, MaxEditDistance); 975 if (EditDistance < BestEditDistance) { 976 BestEditDistance = EditDistance; 977 BestDecl = ND; 978 BestIndex = CurrIndex; 979 } 980 } 981 } // unnamed namespace 982 983 unsigned Sema::correctTypoInParmVarReference( 984 StringRef Typo, 985 ArrayRef<const ParmVarDecl *> ParamVars) { 986 SimpleTypoCorrector Corrector(Typo); 987 for (unsigned i = 0, e = ParamVars.size(); i != e; ++i) 988 Corrector.addDecl(ParamVars[i]); 989 if (Corrector.getBestDecl()) 990 return Corrector.getBestDeclIndex(); 991 else 992 return ParamCommandComment::InvalidParamIndex; 993 } 994 995 namespace { 996 bool ResolveTParamReferenceHelper( 997 StringRef Name, 998 const TemplateParameterList *TemplateParameters, 999 SmallVectorImpl<unsigned> *Position) { 1000 for (unsigned i = 0, e = TemplateParameters->size(); i != e; ++i) { 1001 const NamedDecl *Param = TemplateParameters->getParam(i); 1002 const IdentifierInfo *II = Param->getIdentifier(); 1003 if (II && II->getName() == Name) { 1004 Position->push_back(i); 1005 return true; 1006 } 1007 1008 if (const TemplateTemplateParmDecl *TTP = 1009 dyn_cast<TemplateTemplateParmDecl>(Param)) { 1010 Position->push_back(i); 1011 if (ResolveTParamReferenceHelper(Name, TTP->getTemplateParameters(), 1012 Position)) 1013 return true; 1014 Position->pop_back(); 1015 } 1016 } 1017 return false; 1018 } 1019 } // unnamed namespace 1020 1021 bool Sema::resolveTParamReference( 1022 StringRef Name, 1023 const TemplateParameterList *TemplateParameters, 1024 SmallVectorImpl<unsigned> *Position) { 1025 Position->clear(); 1026 if (!TemplateParameters) 1027 return false; 1028 1029 return ResolveTParamReferenceHelper(Name, TemplateParameters, Position); 1030 } 1031 1032 namespace { 1033 void CorrectTypoInTParamReferenceHelper( 1034 const TemplateParameterList *TemplateParameters, 1035 SimpleTypoCorrector &Corrector) { 1036 for (unsigned i = 0, e = TemplateParameters->size(); i != e; ++i) { 1037 const NamedDecl *Param = TemplateParameters->getParam(i); 1038 Corrector.addDecl(Param); 1039 1040 if (const TemplateTemplateParmDecl *TTP = 1041 dyn_cast<TemplateTemplateParmDecl>(Param)) 1042 CorrectTypoInTParamReferenceHelper(TTP->getTemplateParameters(), 1043 Corrector); 1044 } 1045 } 1046 } // unnamed namespace 1047 1048 StringRef Sema::correctTypoInTParamReference( 1049 StringRef Typo, 1050 const TemplateParameterList *TemplateParameters) { 1051 SimpleTypoCorrector Corrector(Typo); 1052 CorrectTypoInTParamReferenceHelper(TemplateParameters, Corrector); 1053 if (const NamedDecl *ND = Corrector.getBestDecl()) { 1054 const IdentifierInfo *II = ND->getIdentifier(); 1055 assert(II && "SimpleTypoCorrector should not return this decl"); 1056 return II->getName(); 1057 } 1058 return StringRef(); 1059 } 1060 1061 InlineCommandComment::RenderKind 1062 Sema::getInlineCommandRenderKind(StringRef Name) const { 1063 assert(Traits.getCommandInfo(Name)->IsInlineCommand); 1064 1065 return llvm::StringSwitch<InlineCommandComment::RenderKind>(Name) 1066 .Case("b", InlineCommandComment::RenderBold) 1067 .Cases("c", "p", InlineCommandComment::RenderMonospaced) 1068 .Cases("a", "e", "em", InlineCommandComment::RenderEmphasized) 1069 .Default(InlineCommandComment::RenderNormal); 1070 } 1071 1072 } // end namespace comments 1073 } // end namespace clang 1074 1075