Fariborz Jahanian | 43c8df8 | 2013-03-05 19:52:24 +0000 | [diff] [blame] | 1 | //===--- CommentSema.cpp - Doxygen comment semantic analysis --------------===// |
Dmitri Gribenko | ec92531 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 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" |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 11 | #include "clang/AST/Attr.h" |
Dmitri Gribenko | ca7f80a | 2012-08-09 00:03:17 +0000 | [diff] [blame] | 12 | #include "clang/AST/CommentCommandTraits.h" |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 13 | #include "clang/AST/CommentDiagnostic.h" |
Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 14 | #include "clang/AST/Decl.h" |
Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 15 | #include "clang/AST/DeclTemplate.h" |
Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 16 | #include "clang/Basic/SourceManager.h" |
Dmitri Gribenko | 6743e04 | 2012-09-29 11:40:46 +0000 | [diff] [blame] | 17 | #include "clang/Lex/Preprocessor.h" |
Dmitri Gribenko | 6743e04 | 2012-09-29 11:40:46 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallString.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/StringSwitch.h" |
Dmitri Gribenko | ec92531 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 20 | |
| 21 | namespace clang { |
| 22 | namespace comments { |
| 23 | |
Dmitri Gribenko | 3ca956f | 2012-08-31 02:21:44 +0000 | [diff] [blame] | 24 | namespace { |
| 25 | #include "clang/AST/CommentHTMLTagsProperties.inc" |
| 26 | } // unnamed namespace |
| 27 | |
Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 28 | Sema::Sema(llvm::BumpPtrAllocator &Allocator, const SourceManager &SourceMgr, |
Dmitri Gribenko | 6743e04 | 2012-09-29 11:40:46 +0000 | [diff] [blame] | 29 | DiagnosticsEngine &Diags, CommandTraits &Traits, |
| 30 | const Preprocessor *PP) : |
Dmitri Gribenko | ca7f80a | 2012-08-09 00:03:17 +0000 | [diff] [blame] | 31 | Allocator(Allocator), SourceMgr(SourceMgr), Diags(Diags), Traits(Traits), |
Dmitri Gribenko | d666293 | 2013-06-22 23:03:37 +0000 | [diff] [blame] | 32 | PP(PP), ThisDeclInfo(NULL), BriefCommand(NULL), HeaderfileCommand(NULL) { |
Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | void Sema::setDecl(const Decl *D) { |
Dmitri Gribenko | 527ab21 | 2012-08-01 23:08:09 +0000 | [diff] [blame] | 36 | if (!D) |
| 37 | return; |
| 38 | |
| 39 | ThisDeclInfo = new (Allocator) DeclInfo; |
Fariborz Jahanian | 1c883b9 | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 40 | ThisDeclInfo->CommentDecl = D; |
Dmitri Gribenko | e6213dd | 2012-08-01 23:21:57 +0000 | [diff] [blame] | 41 | ThisDeclInfo->IsFilled = false; |
Dmitri Gribenko | ec92531 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | ParagraphComment *Sema::actOnParagraphComment( |
| 45 | ArrayRef<InlineContentComment *> Content) { |
| 46 | return new (Allocator) ParagraphComment(Content); |
| 47 | } |
| 48 | |
Dmitri Gribenko | bcf7f4d | 2013-03-04 23:06:15 +0000 | [diff] [blame] | 49 | BlockCommandComment *Sema::actOnBlockCommandStart( |
| 50 | SourceLocation LocBegin, |
| 51 | SourceLocation LocEnd, |
| 52 | unsigned CommandID, |
| 53 | CommandMarkerKind CommandMarker) { |
Fariborz Jahanian | a649eee | 2013-03-07 23:33:11 +0000 | [diff] [blame] | 54 | BlockCommandComment *BC = new (Allocator) BlockCommandComment(LocBegin, LocEnd, |
| 55 | CommandID, |
| 56 | CommandMarker); |
| 57 | checkContainerDecl(BC); |
| 58 | return BC; |
Dmitri Gribenko | ec92531 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Dmitri Gribenko | a9770ad | 2012-08-06 19:03:12 +0000 | [diff] [blame] | 61 | void Sema::actOnBlockCommandArgs(BlockCommandComment *Command, |
| 62 | ArrayRef<BlockCommandComment::Argument> Args) { |
Dmitri Gribenko | ec92531 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 63 | Command->setArgs(Args); |
Dmitri Gribenko | ec92531 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Dmitri Gribenko | a9770ad | 2012-08-06 19:03:12 +0000 | [diff] [blame] | 66 | void Sema::actOnBlockCommandFinish(BlockCommandComment *Command, |
| 67 | ParagraphComment *Paragraph) { |
Dmitri Gribenko | ec92531 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 68 | Command->setParagraph(Paragraph); |
Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 69 | checkBlockCommandEmptyParagraph(Command); |
Dmitri Gribenko | 5ec0c75 | 2012-08-06 17:08:27 +0000 | [diff] [blame] | 70 | checkBlockCommandDuplicate(Command); |
Dmitri Gribenko | 6bf8f80 | 2014-01-27 17:55:43 +0000 | [diff] [blame] | 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 | } |
Dmitri Gribenko | ec92531 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Dmitri Gribenko | bcf7f4d | 2013-03-04 23:06:15 +0000 | [diff] [blame] | 79 | ParamCommandComment *Sema::actOnParamCommandStart( |
| 80 | SourceLocation LocBegin, |
| 81 | SourceLocation LocEnd, |
| 82 | unsigned CommandID, |
| 83 | CommandMarkerKind CommandMarker) { |
Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 84 | ParamCommandComment *Command = |
Dmitri Gribenko | bcf7f4d | 2013-03-04 23:06:15 +0000 | [diff] [blame] | 85 | new (Allocator) ParamCommandComment(LocBegin, LocEnd, CommandID, |
| 86 | CommandMarker); |
Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 87 | |
Dmitri Gribenko | 4b7f5fe | 2012-07-23 17:40:30 +0000 | [diff] [blame] | 88 | if (!isFunctionDecl()) |
Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 89 | Diag(Command->getLocation(), |
| 90 | diag::warn_doc_param_not_attached_to_a_function_decl) |
Dmitri Gribenko | bcf7f4d | 2013-03-04 23:06:15 +0000 | [diff] [blame] | 91 | << CommandMarker |
Dmitri Gribenko | 7acbf00 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 92 | << Command->getCommandNameRange(Traits); |
Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 93 | |
| 94 | return Command; |
Dmitri Gribenko | ec92531 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Fariborz Jahanian | 8a7a592 | 2013-03-05 01:05:07 +0000 | [diff] [blame] | 97 | void Sema::checkFunctionDeclVerbatimLine(const BlockCommandComment *Comment) { |
| 98 | const CommandInfo *Info = Traits.getCommandInfo(Comment->getCommandID()); |
Fariborz Jahanian | 56fe406 | 2013-03-05 22:46:07 +0000 | [diff] [blame] | 99 | if (!Info->IsFunctionDeclarationCommand) |
| 100 | return; |
Fariborz Jahanian | cfbac5d | 2013-03-08 23:59:23 +0000 | [diff] [blame] | 101 | |
| 102 | unsigned DiagSelect; |
| 103 | switch (Comment->getCommandID()) { |
| 104 | case CommandTraits::KCI_function: |
Fariborz Jahanian | c0607ed | 2013-06-19 18:08:03 +0000 | [diff] [blame] | 105 | DiagSelect = (!isAnyFunctionDecl() && !isFunctionTemplateDecl())? 1 : 0; |
Fariborz Jahanian | cfbac5d | 2013-03-08 23:59:23 +0000 | [diff] [blame] | 106 | break; |
Fariborz Jahanian | abbcbae | 2013-03-18 23:45:52 +0000 | [diff] [blame] | 107 | case CommandTraits::KCI_functiongroup: |
Fariborz Jahanian | c0607ed | 2013-06-19 18:08:03 +0000 | [diff] [blame] | 108 | DiagSelect = (!isAnyFunctionDecl() && !isFunctionTemplateDecl())? 2 : 0; |
Fariborz Jahanian | abbcbae | 2013-03-18 23:45:52 +0000 | [diff] [blame] | 109 | break; |
Fariborz Jahanian | cfbac5d | 2013-03-08 23:59:23 +0000 | [diff] [blame] | 110 | case CommandTraits::KCI_method: |
Fariborz Jahanian | abbcbae | 2013-03-18 23:45:52 +0000 | [diff] [blame] | 111 | DiagSelect = !isObjCMethodDecl() ? 3 : 0; |
| 112 | break; |
| 113 | case CommandTraits::KCI_methodgroup: |
| 114 | DiagSelect = !isObjCMethodDecl() ? 4 : 0; |
Fariborz Jahanian | cfbac5d | 2013-03-08 23:59:23 +0000 | [diff] [blame] | 115 | break; |
| 116 | case CommandTraits::KCI_callback: |
Fariborz Jahanian | abbcbae | 2013-03-18 23:45:52 +0000 | [diff] [blame] | 117 | DiagSelect = !isFunctionPointerVarDecl() ? 5 : 0; |
Fariborz Jahanian | cfbac5d | 2013-03-08 23:59:23 +0000 | [diff] [blame] | 118 | break; |
| 119 | default: |
| 120 | DiagSelect = 0; |
| 121 | break; |
| 122 | } |
Fariborz Jahanian | 41bb713 | 2013-03-06 17:36:51 +0000 | [diff] [blame] | 123 | if (DiagSelect) |
| 124 | Diag(Comment->getLocation(), diag::warn_doc_function_method_decl_mismatch) |
| 125 | << Comment->getCommandMarker() |
| 126 | << (DiagSelect-1) << (DiagSelect-1) |
Fariborz Jahanian | 8a7a592 | 2013-03-05 01:05:07 +0000 | [diff] [blame] | 127 | << Comment->getSourceRange(); |
| 128 | } |
Dmitri Gribenko | 110dfa8 | 2013-12-17 19:28:18 +0000 | [diff] [blame] | 129 | |
Fariborz Jahanian | a649eee | 2013-03-07 23:33:11 +0000 | [diff] [blame] | 130 | void Sema::checkContainerDeclVerbatimLine(const BlockCommandComment *Comment) { |
| 131 | const CommandInfo *Info = Traits.getCommandInfo(Comment->getCommandID()); |
Fariborz Jahanian | cfbac5d | 2013-03-08 23:59:23 +0000 | [diff] [blame] | 132 | if (!Info->IsRecordLikeDeclarationCommand) |
Fariborz Jahanian | a649eee | 2013-03-07 23:33:11 +0000 | [diff] [blame] | 133 | return; |
Fariborz Jahanian | cfbac5d | 2013-03-08 23:59:23 +0000 | [diff] [blame] | 134 | unsigned DiagSelect; |
| 135 | switch (Comment->getCommandID()) { |
| 136 | case CommandTraits::KCI_class: |
Fariborz Jahanian | c0607ed | 2013-06-19 18:08:03 +0000 | [diff] [blame] | 137 | DiagSelect = (!isClassOrStructDecl() && !isClassTemplateDecl()) ? 1 : 0; |
Fariborz Jahanian | 04eb8ab | 2013-05-20 23:40:39 +0000 | [diff] [blame] | 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; |
Fariborz Jahanian | cfbac5d | 2013-03-08 23:59:23 +0000 | [diff] [blame] | 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 | } |
Fariborz Jahanian | a649eee | 2013-03-07 23:33:11 +0000 | [diff] [blame] | 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()); |
Fariborz Jahanian | cfbac5d | 2013-03-08 23:59:23 +0000 | [diff] [blame] | 169 | if (!Info->IsRecordLikeDetailCommand || isRecordLikeDecl()) |
Fariborz Jahanian | a649eee | 2013-03-07 23:33:11 +0000 | [diff] [blame] | 170 | return; |
Fariborz Jahanian | cfbac5d | 2013-03-08 23:59:23 +0000 | [diff] [blame] | 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 | } |
Fariborz Jahanian | a649eee | 2013-03-07 23:33:11 +0000 | [diff] [blame] | 210 | if (DiagSelect) |
| 211 | Diag(Comment->getLocation(), diag::warn_doc_container_decl_mismatch) |
| 212 | << Comment->getCommandMarker() |
| 213 | << (DiagSelect-1) |
| 214 | << Comment->getSourceRange(); |
| 215 | } |
Fariborz Jahanian | 8a7a592 | 2013-03-05 01:05:07 +0000 | [diff] [blame] | 216 | |
Benjamin Kramer | 70e2e17 | 2013-11-10 16:26:43 +0000 | [diff] [blame] | 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 | |
Dmitri Gribenko | a9770ad | 2012-08-06 19:03:12 +0000 | [diff] [blame] | 227 | void Sema::actOnParamCommandDirectionArg(ParamCommandComment *Command, |
| 228 | SourceLocation ArgLocBegin, |
| 229 | SourceLocation ArgLocEnd, |
| 230 | StringRef Arg) { |
Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 231 | std::string ArgLower = Arg.lower(); |
Benjamin Kramer | 70e2e17 | 2013-11-10 16:26:43 +0000 | [diff] [blame] | 232 | int Direction = getParamPassDirection(ArgLower); |
Dmitri Gribenko | ec92531 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 233 | |
Benjamin Kramer | 70e2e17 | 2013-11-10 16:26:43 +0000 | [diff] [blame] | 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); |
Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 240 | |
| 241 | SourceRange ArgRange(ArgLocBegin, ArgLocEnd); |
Benjamin Kramer | 70e2e17 | 2013-11-10 16:26:43 +0000 | [diff] [blame] | 242 | if (Direction != -1) { |
| 243 | const char *FixedName = ParamCommandComment::getDirectionAsString( |
| 244 | (ParamCommandComment::PassDirection)Direction); |
Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 245 | Diag(ArgLocBegin, diag::warn_doc_param_spaces_in_direction) |
Benjamin Kramer | 70e2e17 | 2013-11-10 16:26:43 +0000 | [diff] [blame] | 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 | } |
Dmitri Gribenko | ec92531 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 251 | } |
Benjamin Kramer | 70e2e17 | 2013-11-10 16:26:43 +0000 | [diff] [blame] | 252 | Command->setDirection((ParamCommandComment::PassDirection)Direction, |
| 253 | /*Explicit=*/true); |
Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Dmitri Gribenko | a9770ad | 2012-08-06 19:03:12 +0000 | [diff] [blame] | 256 | void Sema::actOnParamCommandParamNameArg(ParamCommandComment *Command, |
| 257 | SourceLocation ArgLocBegin, |
| 258 | SourceLocation ArgLocEnd, |
| 259 | StringRef Arg) { |
Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 260 | // Parser will not feed us more arguments than needed. |
Dmitri Gribenko | 619e75e | 2012-07-13 19:02:42 +0000 | [diff] [blame] | 261 | assert(Command->getNumArgs() == 0); |
Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 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)); |
Dmitri Gribenko | ec92531 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 272 | } |
| 273 | |
Dmitri Gribenko | a9770ad | 2012-08-06 19:03:12 +0000 | [diff] [blame] | 274 | void Sema::actOnParamCommandFinish(ParamCommandComment *Command, |
| 275 | ParagraphComment *Paragraph) { |
Dmitri Gribenko | ec92531 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 276 | Command->setParagraph(Paragraph); |
Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 277 | checkBlockCommandEmptyParagraph(Command); |
Dmitri Gribenko | ec92531 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 278 | } |
| 279 | |
Dmitri Gribenko | bcf7f4d | 2013-03-04 23:06:15 +0000 | [diff] [blame] | 280 | TParamCommandComment *Sema::actOnTParamCommandStart( |
| 281 | SourceLocation LocBegin, |
| 282 | SourceLocation LocEnd, |
| 283 | unsigned CommandID, |
| 284 | CommandMarkerKind CommandMarker) { |
Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 285 | TParamCommandComment *Command = |
Dmitri Gribenko | bcf7f4d | 2013-03-04 23:06:15 +0000 | [diff] [blame] | 286 | new (Allocator) TParamCommandComment(LocBegin, LocEnd, CommandID, |
| 287 | CommandMarker); |
Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 288 | |
Dmitri Gribenko | 8e5d5f1 | 2012-08-06 21:31:15 +0000 | [diff] [blame] | 289 | if (!isTemplateOrSpecialization()) |
Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 290 | Diag(Command->getLocation(), |
| 291 | diag::warn_doc_tparam_not_attached_to_a_template_decl) |
Dmitri Gribenko | bcf7f4d | 2013-03-04 23:06:15 +0000 | [diff] [blame] | 292 | << CommandMarker |
Dmitri Gribenko | 7acbf00 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 293 | << Command->getCommandNameRange(Traits); |
Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 294 | |
| 295 | return Command; |
| 296 | } |
| 297 | |
Dmitri Gribenko | a9770ad | 2012-08-06 19:03:12 +0000 | [diff] [blame] | 298 | void Sema::actOnTParamCommandParamNameArg(TParamCommandComment *Command, |
| 299 | SourceLocation ArgLocBegin, |
| 300 | SourceLocation ArgLocEnd, |
| 301 | StringRef Arg) { |
Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 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 | |
Dmitri Gribenko | 8e5d5f1 | 2012-08-06 21:31:15 +0000 | [diff] [blame] | 311 | if (!isTemplateOrSpecialization()) { |
Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 312 | // We already warned that this \\tparam is not attached to a template decl. |
Dmitri Gribenko | a9770ad | 2012-08-06 19:03:12 +0000 | [diff] [blame] | 313 | return; |
Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 314 | } |
| 315 | |
Dmitri Gribenko | 527ab21 | 2012-08-01 23:08:09 +0000 | [diff] [blame] | 316 | const TemplateParameterList *TemplateParameters = |
| 317 | ThisDeclInfo->TemplateParameters; |
Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 318 | SmallVector<unsigned, 2> Position; |
| 319 | if (resolveTParamReference(Arg, TemplateParameters, &Position)) { |
| 320 | Command->setPosition(copyArray(llvm::makeArrayRef(Position))); |
Benjamin Kramer | eed8061 | 2013-11-10 16:55:11 +0000 | [diff] [blame] | 321 | TParamCommandComment *&PrevCommand = TemplateParameterDocs[Arg]; |
| 322 | if (PrevCommand) { |
Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 323 | SourceRange ArgRange(ArgLocBegin, ArgLocEnd); |
| 324 | Diag(ArgLocBegin, diag::warn_doc_tparam_duplicate) |
| 325 | << Arg << ArgRange; |
Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 326 | Diag(PrevCommand->getLocation(), diag::note_doc_tparam_previous) |
| 327 | << PrevCommand->getParamNameRange(); |
| 328 | } |
Benjamin Kramer | eed8061 | 2013-11-10 16:55:11 +0000 | [diff] [blame] | 329 | PrevCommand = Command; |
Dmitri Gribenko | a9770ad | 2012-08-06 19:03:12 +0000 | [diff] [blame] | 330 | return; |
Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 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) |
Dmitri Gribenko | a9770ad | 2012-08-06 19:03:12 +0000 | [diff] [blame] | 338 | return; |
Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 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 | |
Dmitri Gribenko | a9770ad | 2012-08-06 19:03:12 +0000 | [diff] [blame] | 356 | return; |
Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 357 | } |
| 358 | |
Dmitri Gribenko | a9770ad | 2012-08-06 19:03:12 +0000 | [diff] [blame] | 359 | void Sema::actOnTParamCommandFinish(TParamCommandComment *Command, |
| 360 | ParagraphComment *Paragraph) { |
Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 361 | Command->setParagraph(Paragraph); |
| 362 | checkBlockCommandEmptyParagraph(Command); |
Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 363 | } |
| 364 | |
Dmitri Gribenko | ec92531 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 365 | InlineCommandComment *Sema::actOnInlineCommand(SourceLocation CommandLocBegin, |
| 366 | SourceLocation CommandLocEnd, |
Dmitri Gribenko | 7acbf00 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 367 | unsigned CommandID) { |
Dmitri Gribenko | ec92531 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 368 | ArrayRef<InlineCommandComment::Argument> Args; |
Dmitri Gribenko | 7acbf00 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 369 | StringRef CommandName = Traits.getCommandInfo(CommandID)->Name; |
Dmitri Gribenko | d73e4ce | 2012-07-23 16:43:01 +0000 | [diff] [blame] | 370 | return new (Allocator) InlineCommandComment( |
| 371 | CommandLocBegin, |
| 372 | CommandLocEnd, |
Dmitri Gribenko | 7acbf00 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 373 | CommandID, |
Dmitri Gribenko | d73e4ce | 2012-07-23 16:43:01 +0000 | [diff] [blame] | 374 | getInlineCommandRenderKind(CommandName), |
| 375 | Args); |
Dmitri Gribenko | ec92531 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | InlineCommandComment *Sema::actOnInlineCommand(SourceLocation CommandLocBegin, |
| 379 | SourceLocation CommandLocEnd, |
Dmitri Gribenko | 7acbf00 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 380 | unsigned CommandID, |
Dmitri Gribenko | ec92531 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 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); |
Dmitri Gribenko | 7acbf00 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 388 | StringRef CommandName = Traits.getCommandInfo(CommandID)->Name; |
Dmitri Gribenko | ec92531 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 389 | |
Dmitri Gribenko | d73e4ce | 2012-07-23 16:43:01 +0000 | [diff] [blame] | 390 | return new (Allocator) InlineCommandComment( |
| 391 | CommandLocBegin, |
| 392 | CommandLocEnd, |
Dmitri Gribenko | 7acbf00 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 393 | CommandID, |
Dmitri Gribenko | d73e4ce | 2012-07-23 16:43:01 +0000 | [diff] [blame] | 394 | getInlineCommandRenderKind(CommandName), |
| 395 | llvm::makeArrayRef(A, 1)); |
Dmitri Gribenko | ec92531 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | InlineContentComment *Sema::actOnUnknownCommand(SourceLocation LocBegin, |
| 399 | SourceLocation LocEnd, |
Dmitri Gribenko | 9304d86 | 2012-09-11 19:22:03 +0000 | [diff] [blame] | 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) { |
Dmitri Gribenko | ec92531 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 408 | ArrayRef<InlineCommandComment::Argument> Args; |
Dmitri Gribenko | d73e4ce | 2012-07-23 16:43:01 +0000 | [diff] [blame] | 409 | return new (Allocator) InlineCommandComment( |
Dmitri Gribenko | 7acbf00 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 410 | LocBegin, LocEnd, CommandID, |
Dmitri Gribenko | d73e4ce | 2012-07-23 16:43:01 +0000 | [diff] [blame] | 411 | InlineCommandComment::RenderNormal, |
| 412 | Args); |
Dmitri Gribenko | ec92531 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 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, |
Dmitri Gribenko | 7acbf00 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 422 | unsigned CommandID) { |
| 423 | StringRef CommandName = Traits.getCommandInfo(CommandID)->Name; |
Dmitri Gribenko | ec92531 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 424 | return new (Allocator) VerbatimBlockComment( |
| 425 | Loc, |
Dmitri Gribenko | 7acbf00 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 426 | Loc.getLocWithOffset(1 + CommandName.size()), |
| 427 | CommandID); |
Dmitri Gribenko | ec92531 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | VerbatimBlockLineComment *Sema::actOnVerbatimBlockLine(SourceLocation Loc, |
| 431 | StringRef Text) { |
| 432 | return new (Allocator) VerbatimBlockLineComment(Loc, Text); |
| 433 | } |
| 434 | |
Dmitri Gribenko | a9770ad | 2012-08-06 19:03:12 +0000 | [diff] [blame] | 435 | void Sema::actOnVerbatimBlockFinish( |
Dmitri Gribenko | ec92531 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 436 | VerbatimBlockComment *Block, |
| 437 | SourceLocation CloseNameLocBegin, |
| 438 | StringRef CloseName, |
| 439 | ArrayRef<VerbatimBlockLineComment *> Lines) { |
| 440 | Block->setCloseName(CloseName, CloseNameLocBegin); |
| 441 | Block->setLines(Lines); |
Dmitri Gribenko | ec92531 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | VerbatimLineComment *Sema::actOnVerbatimLine(SourceLocation LocBegin, |
Dmitri Gribenko | 7acbf00 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 445 | unsigned CommandID, |
Dmitri Gribenko | ec92531 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 446 | SourceLocation TextBegin, |
| 447 | StringRef Text) { |
Fariborz Jahanian | f4ba35d | 2013-03-05 19:40:47 +0000 | [diff] [blame] | 448 | VerbatimLineComment *VL = new (Allocator) VerbatimLineComment( |
Dmitri Gribenko | ec92531 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 449 | LocBegin, |
| 450 | TextBegin.getLocWithOffset(Text.size()), |
Dmitri Gribenko | 7acbf00 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 451 | CommandID, |
Dmitri Gribenko | ec92531 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 452 | TextBegin, |
| 453 | Text); |
Fariborz Jahanian | f4ba35d | 2013-03-05 19:40:47 +0000 | [diff] [blame] | 454 | checkFunctionDeclVerbatimLine(VL); |
Fariborz Jahanian | a649eee | 2013-03-07 23:33:11 +0000 | [diff] [blame] | 455 | checkContainerDeclVerbatimLine(VL); |
Fariborz Jahanian | f4ba35d | 2013-03-05 19:40:47 +0000 | [diff] [blame] | 456 | return VL; |
Dmitri Gribenko | ec92531 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 457 | } |
| 458 | |
Dmitri Gribenko | e00ffc7 | 2012-07-13 00:44:24 +0000 | [diff] [blame] | 459 | HTMLStartTagComment *Sema::actOnHTMLStartTagStart(SourceLocation LocBegin, |
| 460 | StringRef TagName) { |
| 461 | return new (Allocator) HTMLStartTagComment(LocBegin, TagName); |
Dmitri Gribenko | ec92531 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 462 | } |
| 463 | |
Dmitri Gribenko | a9770ad | 2012-08-06 19:03:12 +0000 | [diff] [blame] | 464 | void Sema::actOnHTMLStartTagFinish( |
Dmitri Gribenko | e00ffc7 | 2012-07-13 00:44:24 +0000 | [diff] [blame] | 465 | HTMLStartTagComment *Tag, |
| 466 | ArrayRef<HTMLStartTagComment::Attribute> Attrs, |
Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 467 | SourceLocation GreaterLoc, |
| 468 | bool IsSelfClosing) { |
Dmitri Gribenko | ec92531 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 469 | Tag->setAttrs(Attrs); |
Dmitri Gribenko | 9304362 | 2014-04-22 10:59:13 +0000 | [diff] [blame^] | 470 | for (const auto &Attr : Attrs) { |
| 471 | if (!isHTMLAttributeSafeToPassThrough(Attr.Name)) |
| 472 | Tag->setUnsafeToPassThrough(); |
| 473 | } |
| 474 | |
Dmitri Gribenko | ec92531 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 475 | Tag->setGreaterLoc(GreaterLoc); |
Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 476 | if (IsSelfClosing) |
| 477 | Tag->setSelfClosing(); |
Dmitri Gribenko | e00ffc7 | 2012-07-13 00:44:24 +0000 | [diff] [blame] | 478 | else if (!isHTMLEndTagForbidden(Tag->getTagName())) |
Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 479 | HTMLOpenTags.push_back(Tag); |
Dmitri Gribenko | ec92531 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 480 | } |
| 481 | |
Dmitri Gribenko | e00ffc7 | 2012-07-13 00:44:24 +0000 | [diff] [blame] | 482 | HTMLEndTagComment *Sema::actOnHTMLEndTag(SourceLocation LocBegin, |
| 483 | SourceLocation LocEnd, |
| 484 | StringRef TagName) { |
| 485 | HTMLEndTagComment *HET = |
| 486 | new (Allocator) HTMLEndTagComment(LocBegin, LocEnd, TagName); |
| 487 | if (isHTMLEndTagForbidden(TagName)) { |
| 488 | Diag(HET->getLocation(), diag::warn_doc_html_end_forbidden) |
| 489 | << TagName << HET->getSourceRange(); |
Dmitri Gribenko | 9304362 | 2014-04-22 10:59:13 +0000 | [diff] [blame^] | 490 | HET->setUnsafeToPassThrough(); |
Dmitri Gribenko | e00ffc7 | 2012-07-13 00:44:24 +0000 | [diff] [blame] | 491 | return HET; |
Dmitri Gribenko | 9460fbf | 2012-07-12 23:37:09 +0000 | [diff] [blame] | 492 | } |
| 493 | |
Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 494 | bool FoundOpen = false; |
Dmitri Gribenko | e00ffc7 | 2012-07-13 00:44:24 +0000 | [diff] [blame] | 495 | for (SmallVectorImpl<HTMLStartTagComment *>::const_reverse_iterator |
Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 496 | I = HTMLOpenTags.rbegin(), E = HTMLOpenTags.rend(); |
| 497 | I != E; ++I) { |
| 498 | if ((*I)->getTagName() == TagName) { |
| 499 | FoundOpen = true; |
| 500 | break; |
| 501 | } |
| 502 | } |
| 503 | if (!FoundOpen) { |
Dmitri Gribenko | e00ffc7 | 2012-07-13 00:44:24 +0000 | [diff] [blame] | 504 | Diag(HET->getLocation(), diag::warn_doc_html_end_unbalanced) |
| 505 | << HET->getSourceRange(); |
Dmitri Gribenko | 9304362 | 2014-04-22 10:59:13 +0000 | [diff] [blame^] | 506 | HET->setUnsafeToPassThrough(); |
Dmitri Gribenko | e00ffc7 | 2012-07-13 00:44:24 +0000 | [diff] [blame] | 507 | return HET; |
Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | while (!HTMLOpenTags.empty()) { |
Dmitri Gribenko | 9304362 | 2014-04-22 10:59:13 +0000 | [diff] [blame^] | 511 | HTMLStartTagComment *HST = HTMLOpenTags.pop_back_val(); |
Dmitri Gribenko | e00ffc7 | 2012-07-13 00:44:24 +0000 | [diff] [blame] | 512 | StringRef LastNotClosedTagName = HST->getTagName(); |
Dmitri Gribenko | 9304362 | 2014-04-22 10:59:13 +0000 | [diff] [blame^] | 513 | if (LastNotClosedTagName == TagName) { |
| 514 | // If the start tag is unsafe, end tag is unsafe as well. |
| 515 | if (!HST->isSafeToPassThrough()) |
| 516 | HET->setUnsafeToPassThrough(); |
Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 517 | break; |
Dmitri Gribenko | 9304362 | 2014-04-22 10:59:13 +0000 | [diff] [blame^] | 518 | } |
Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 519 | |
Dmitri Gribenko | e00ffc7 | 2012-07-13 00:44:24 +0000 | [diff] [blame] | 520 | if (isHTMLEndTagOptional(LastNotClosedTagName)) |
Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 521 | continue; |
| 522 | |
| 523 | bool OpenLineInvalid; |
| 524 | const unsigned OpenLine = SourceMgr.getPresumedLineNumber( |
Dmitri Gribenko | e00ffc7 | 2012-07-13 00:44:24 +0000 | [diff] [blame] | 525 | HST->getLocation(), |
Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 526 | &OpenLineInvalid); |
| 527 | bool CloseLineInvalid; |
| 528 | const unsigned CloseLine = SourceMgr.getPresumedLineNumber( |
Dmitri Gribenko | e00ffc7 | 2012-07-13 00:44:24 +0000 | [diff] [blame] | 529 | HET->getLocation(), |
Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 530 | &CloseLineInvalid); |
| 531 | |
Dmitri Gribenko | 9304362 | 2014-04-22 10:59:13 +0000 | [diff] [blame^] | 532 | if (OpenLineInvalid || CloseLineInvalid || OpenLine == CloseLine) { |
Dmitri Gribenko | e00ffc7 | 2012-07-13 00:44:24 +0000 | [diff] [blame] | 533 | Diag(HST->getLocation(), diag::warn_doc_html_start_end_mismatch) |
| 534 | << HST->getTagName() << HET->getTagName() |
| 535 | << HST->getSourceRange() << HET->getSourceRange(); |
Dmitri Gribenko | 9304362 | 2014-04-22 10:59:13 +0000 | [diff] [blame^] | 536 | HST->setUnsafeToPassThrough(); |
| 537 | } else { |
Dmitri Gribenko | e00ffc7 | 2012-07-13 00:44:24 +0000 | [diff] [blame] | 538 | Diag(HST->getLocation(), diag::warn_doc_html_start_end_mismatch) |
| 539 | << HST->getTagName() << HET->getTagName() |
| 540 | << HST->getSourceRange(); |
| 541 | Diag(HET->getLocation(), diag::note_doc_html_end_tag) |
| 542 | << HET->getSourceRange(); |
Dmitri Gribenko | 9304362 | 2014-04-22 10:59:13 +0000 | [diff] [blame^] | 543 | HST->setUnsafeToPassThrough(); |
Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 544 | } |
| 545 | } |
| 546 | |
Dmitri Gribenko | e00ffc7 | 2012-07-13 00:44:24 +0000 | [diff] [blame] | 547 | return HET; |
Dmitri Gribenko | ec92531 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | FullComment *Sema::actOnFullComment( |
| 551 | ArrayRef<BlockContentComment *> Blocks) { |
Fariborz Jahanian | 42e3132 | 2012-10-11 23:52:50 +0000 | [diff] [blame] | 552 | FullComment *FC = new (Allocator) FullComment(Blocks, ThisDeclInfo); |
Dmitri Gribenko | 219bd15 | 2012-08-24 17:45:39 +0000 | [diff] [blame] | 553 | resolveParamCommandIndexes(FC); |
Dmitri Gribenko | 9304362 | 2014-04-22 10:59:13 +0000 | [diff] [blame^] | 554 | |
| 555 | // Complain about HTML tags that are not closed. |
| 556 | while (!HTMLOpenTags.empty()) { |
| 557 | HTMLStartTagComment *HST = HTMLOpenTags.pop_back_val(); |
| 558 | if (isHTMLEndTagOptional(HST->getTagName())) |
| 559 | continue; |
| 560 | |
| 561 | Diag(HST->getLocation(), diag::warn_doc_html_missing_end_tag) |
| 562 | << HST->getTagName() << HST->getSourceRange(); |
| 563 | HST->setUnsafeToPassThrough(); |
| 564 | } |
| 565 | |
Dmitri Gribenko | 219bd15 | 2012-08-24 17:45:39 +0000 | [diff] [blame] | 566 | return FC; |
Dmitri Gribenko | ec92531 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 567 | } |
| 568 | |
Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 569 | void Sema::checkBlockCommandEmptyParagraph(BlockCommandComment *Command) { |
Dmitri Gribenko | b37d5e8 | 2012-09-13 20:36:01 +0000 | [diff] [blame] | 570 | if (Traits.getCommandInfo(Command->getCommandID())->IsEmptyParagraphAllowed) |
| 571 | return; |
| 572 | |
Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 573 | ParagraphComment *Paragraph = Command->getParagraph(); |
| 574 | if (Paragraph->isWhitespace()) { |
| 575 | SourceLocation DiagLoc; |
Dmitri Gribenko | 619e75e | 2012-07-13 19:02:42 +0000 | [diff] [blame] | 576 | if (Command->getNumArgs() > 0) |
| 577 | DiagLoc = Command->getArgRange(Command->getNumArgs() - 1).getEnd(); |
Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 578 | if (!DiagLoc.isValid()) |
Dmitri Gribenko | 7acbf00 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 579 | DiagLoc = Command->getCommandNameRange(Traits).getEnd(); |
Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 580 | Diag(DiagLoc, diag::warn_doc_block_command_empty_paragraph) |
Dmitri Gribenko | bcf7f4d | 2013-03-04 23:06:15 +0000 | [diff] [blame] | 581 | << Command->getCommandMarker() |
Dmitri Gribenko | 7acbf00 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 582 | << Command->getCommandName(Traits) |
Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 583 | << Command->getSourceRange(); |
| 584 | } |
| 585 | } |
| 586 | |
Dmitri Gribenko | 6430583 | 2012-08-03 21:15:32 +0000 | [diff] [blame] | 587 | void Sema::checkReturnsCommand(const BlockCommandComment *Command) { |
Dmitri Gribenko | 7acbf00 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 588 | if (!Traits.getCommandInfo(Command->getCommandID())->IsReturnsCommand) |
Dmitri Gribenko | 6430583 | 2012-08-03 21:15:32 +0000 | [diff] [blame] | 589 | return; |
Dmitri Gribenko | 6bf8f80 | 2014-01-27 17:55:43 +0000 | [diff] [blame] | 590 | |
| 591 | assert(ThisDeclInfo && "should not call this check on a bare comment"); |
| 592 | |
Dmitri Gribenko | 6430583 | 2012-08-03 21:15:32 +0000 | [diff] [blame] | 593 | if (isFunctionDecl()) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 594 | if (ThisDeclInfo->ReturnType->isVoidType()) { |
Dmitri Gribenko | 6430583 | 2012-08-03 21:15:32 +0000 | [diff] [blame] | 595 | unsigned DiagKind; |
Fariborz Jahanian | 1c883b9 | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 596 | switch (ThisDeclInfo->CommentDecl->getKind()) { |
Dmitri Gribenko | 6430583 | 2012-08-03 21:15:32 +0000 | [diff] [blame] | 597 | default: |
Dmitri Gribenko | 558babc | 2012-08-06 16:29:26 +0000 | [diff] [blame] | 598 | if (ThisDeclInfo->IsObjCMethod) |
| 599 | DiagKind = 3; |
| 600 | else |
| 601 | DiagKind = 0; |
Dmitri Gribenko | 6430583 | 2012-08-03 21:15:32 +0000 | [diff] [blame] | 602 | break; |
| 603 | case Decl::CXXConstructor: |
| 604 | DiagKind = 1; |
| 605 | break; |
| 606 | case Decl::CXXDestructor: |
| 607 | DiagKind = 2; |
| 608 | break; |
| 609 | } |
| 610 | Diag(Command->getLocation(), |
| 611 | diag::warn_doc_returns_attached_to_a_void_function) |
Dmitri Gribenko | bcf7f4d | 2013-03-04 23:06:15 +0000 | [diff] [blame] | 612 | << Command->getCommandMarker() |
Dmitri Gribenko | 7acbf00 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 613 | << Command->getCommandName(Traits) |
Dmitri Gribenko | 6430583 | 2012-08-03 21:15:32 +0000 | [diff] [blame] | 614 | << DiagKind |
| 615 | << Command->getSourceRange(); |
| 616 | } |
| 617 | return; |
| 618 | } |
Fariborz Jahanian | 81bbee1 | 2013-02-27 00:46:06 +0000 | [diff] [blame] | 619 | else if (isObjCPropertyDecl()) |
| 620 | return; |
Dmitri Gribenko | 110dfa8 | 2013-12-17 19:28:18 +0000 | [diff] [blame] | 621 | |
Dmitri Gribenko | 6430583 | 2012-08-03 21:15:32 +0000 | [diff] [blame] | 622 | Diag(Command->getLocation(), |
| 623 | diag::warn_doc_returns_not_attached_to_a_function_decl) |
Dmitri Gribenko | bcf7f4d | 2013-03-04 23:06:15 +0000 | [diff] [blame] | 624 | << Command->getCommandMarker() |
Dmitri Gribenko | 7acbf00 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 625 | << Command->getCommandName(Traits) |
Dmitri Gribenko | 6430583 | 2012-08-03 21:15:32 +0000 | [diff] [blame] | 626 | << Command->getSourceRange(); |
| 627 | } |
| 628 | |
Dmitri Gribenko | 5ec0c75 | 2012-08-06 17:08:27 +0000 | [diff] [blame] | 629 | void Sema::checkBlockCommandDuplicate(const BlockCommandComment *Command) { |
Dmitri Gribenko | 7acbf00 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 630 | const CommandInfo *Info = Traits.getCommandInfo(Command->getCommandID()); |
Dmitri Gribenko | 5ec0c75 | 2012-08-06 17:08:27 +0000 | [diff] [blame] | 631 | const BlockCommandComment *PrevCommand = NULL; |
Dmitri Gribenko | 7acbf00 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 632 | if (Info->IsBriefCommand) { |
Dmitri Gribenko | 5ec0c75 | 2012-08-06 17:08:27 +0000 | [diff] [blame] | 633 | if (!BriefCommand) { |
| 634 | BriefCommand = Command; |
| 635 | return; |
| 636 | } |
| 637 | PrevCommand = BriefCommand; |
Fariborz Jahanian | 1a0cf80 | 2013-01-31 23:12:39 +0000 | [diff] [blame] | 638 | } else if (Info->IsHeaderfileCommand) { |
| 639 | if (!HeaderfileCommand) { |
| 640 | HeaderfileCommand = Command; |
| 641 | return; |
| 642 | } |
| 643 | PrevCommand = HeaderfileCommand; |
Dmitri Gribenko | 5ec0c75 | 2012-08-06 17:08:27 +0000 | [diff] [blame] | 644 | } else { |
| 645 | // We don't want to check this command for duplicates. |
| 646 | return; |
| 647 | } |
Dmitri Gribenko | 7acbf00 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 648 | StringRef CommandName = Command->getCommandName(Traits); |
| 649 | StringRef PrevCommandName = PrevCommand->getCommandName(Traits); |
Dmitri Gribenko | 5ec0c75 | 2012-08-06 17:08:27 +0000 | [diff] [blame] | 650 | Diag(Command->getLocation(), diag::warn_doc_block_command_duplicate) |
Dmitri Gribenko | bcf7f4d | 2013-03-04 23:06:15 +0000 | [diff] [blame] | 651 | << Command->getCommandMarker() |
Dmitri Gribenko | 7acbf00 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 652 | << CommandName |
Dmitri Gribenko | 5ec0c75 | 2012-08-06 17:08:27 +0000 | [diff] [blame] | 653 | << Command->getSourceRange(); |
Dmitri Gribenko | 7acbf00 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 654 | if (CommandName == PrevCommandName) |
Dmitri Gribenko | 5ec0c75 | 2012-08-06 17:08:27 +0000 | [diff] [blame] | 655 | Diag(PrevCommand->getLocation(), diag::note_doc_block_command_previous) |
Dmitri Gribenko | bcf7f4d | 2013-03-04 23:06:15 +0000 | [diff] [blame] | 656 | << PrevCommand->getCommandMarker() |
Dmitri Gribenko | 7acbf00 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 657 | << PrevCommandName |
| 658 | << PrevCommand->getSourceRange(); |
Dmitri Gribenko | 5ec0c75 | 2012-08-06 17:08:27 +0000 | [diff] [blame] | 659 | else |
| 660 | Diag(PrevCommand->getLocation(), |
| 661 | diag::note_doc_block_command_previous_alias) |
Dmitri Gribenko | bcf7f4d | 2013-03-04 23:06:15 +0000 | [diff] [blame] | 662 | << PrevCommand->getCommandMarker() |
Dmitri Gribenko | 7acbf00 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 663 | << PrevCommandName |
| 664 | << CommandName; |
Dmitri Gribenko | 5ec0c75 | 2012-08-06 17:08:27 +0000 | [diff] [blame] | 665 | } |
| 666 | |
Dmitri Gribenko | 1da8886 | 2012-09-22 21:47:50 +0000 | [diff] [blame] | 667 | void Sema::checkDeprecatedCommand(const BlockCommandComment *Command) { |
| 668 | if (!Traits.getCommandInfo(Command->getCommandID())->IsDeprecatedCommand) |
| 669 | return; |
| 670 | |
Dmitri Gribenko | 6bf8f80 | 2014-01-27 17:55:43 +0000 | [diff] [blame] | 671 | assert(ThisDeclInfo && "should not call this check on a bare comment"); |
| 672 | |
Fariborz Jahanian | 1c883b9 | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 673 | const Decl *D = ThisDeclInfo->CommentDecl; |
Dmitri Gribenko | 1da8886 | 2012-09-22 21:47:50 +0000 | [diff] [blame] | 674 | if (!D) |
| 675 | return; |
| 676 | |
| 677 | if (D->hasAttr<DeprecatedAttr>() || |
| 678 | D->hasAttr<AvailabilityAttr>() || |
| 679 | D->hasAttr<UnavailableAttr>()) |
| 680 | return; |
| 681 | |
| 682 | Diag(Command->getLocation(), |
| 683 | diag::warn_doc_deprecated_not_sync) |
| 684 | << Command->getSourceRange(); |
| 685 | |
| 686 | // Try to emit a fixit with a deprecation attribute. |
| 687 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 688 | // Don't emit a Fix-It for non-member function definitions. GCC does not |
| 689 | // accept attributes on them. |
| 690 | const DeclContext *Ctx = FD->getDeclContext(); |
| 691 | if ((!Ctx || !Ctx->isRecord()) && |
| 692 | FD->doesThisDeclarationHaveABody()) |
| 693 | return; |
| 694 | |
Dmitri Gribenko | 6743e04 | 2012-09-29 11:40:46 +0000 | [diff] [blame] | 695 | StringRef AttributeSpelling = "__attribute__((deprecated))"; |
| 696 | if (PP) { |
| 697 | TokenValue Tokens[] = { |
| 698 | tok::kw___attribute, tok::l_paren, tok::l_paren, |
| 699 | PP->getIdentifierInfo("deprecated"), |
| 700 | tok::r_paren, tok::r_paren |
| 701 | }; |
| 702 | StringRef MacroName = PP->getLastMacroWithSpelling(FD->getLocation(), |
| 703 | Tokens); |
| 704 | if (!MacroName.empty()) |
| 705 | AttributeSpelling = MacroName; |
| 706 | } |
| 707 | |
| 708 | SmallString<64> TextToInsert(" "); |
| 709 | TextToInsert += AttributeSpelling; |
Dmitri Gribenko | 1da8886 | 2012-09-22 21:47:50 +0000 | [diff] [blame] | 710 | Diag(FD->getLocEnd(), |
| 711 | diag::note_add_deprecation_attr) |
| 712 | << FixItHint::CreateInsertion(FD->getLocEnd().getLocWithOffset(1), |
Dmitri Gribenko | 6743e04 | 2012-09-29 11:40:46 +0000 | [diff] [blame] | 713 | TextToInsert); |
Dmitri Gribenko | 1da8886 | 2012-09-22 21:47:50 +0000 | [diff] [blame] | 714 | } |
| 715 | } |
| 716 | |
Dmitri Gribenko | 219bd15 | 2012-08-24 17:45:39 +0000 | [diff] [blame] | 717 | void Sema::resolveParamCommandIndexes(const FullComment *FC) { |
| 718 | if (!isFunctionDecl()) { |
| 719 | // We already warned that \\param commands are not attached to a function |
| 720 | // decl. |
| 721 | return; |
| 722 | } |
| 723 | |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 724 | SmallVector<ParamCommandComment *, 8> UnresolvedParamCommands; |
Dmitri Gribenko | 219bd15 | 2012-08-24 17:45:39 +0000 | [diff] [blame] | 725 | |
| 726 | // Comment AST nodes that correspond to \c ParamVars for which we have |
| 727 | // found a \\param command or NULL if no documentation was found so far. |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 728 | SmallVector<ParamCommandComment *, 8> ParamVarDocs; |
Dmitri Gribenko | 219bd15 | 2012-08-24 17:45:39 +0000 | [diff] [blame] | 729 | |
| 730 | ArrayRef<const ParmVarDecl *> ParamVars = getParamVars(); |
NAKAMURA Takumi | dc2e2fb | 2013-06-19 06:58:14 +0000 | [diff] [blame] | 731 | ParamVarDocs.resize(ParamVars.size(), NULL); |
Dmitri Gribenko | 219bd15 | 2012-08-24 17:45:39 +0000 | [diff] [blame] | 732 | |
| 733 | // First pass over all \\param commands: resolve all parameter names. |
| 734 | for (Comment::child_iterator I = FC->child_begin(), E = FC->child_end(); |
| 735 | I != E; ++I) { |
| 736 | ParamCommandComment *PCC = dyn_cast<ParamCommandComment>(*I); |
| 737 | if (!PCC || !PCC->hasParamName()) |
| 738 | continue; |
Fariborz Jahanian | 9d2f1e7 | 2012-10-18 21:42:42 +0000 | [diff] [blame] | 739 | StringRef ParamName = PCC->getParamNameAsWritten(); |
Dmitri Gribenko | 219bd15 | 2012-08-24 17:45:39 +0000 | [diff] [blame] | 740 | |
| 741 | // Check that referenced parameter name is in the function decl. |
| 742 | const unsigned ResolvedParamIndex = resolveParmVarReference(ParamName, |
| 743 | ParamVars); |
Dmitri Gribenko | 02489eb | 2013-06-24 04:41:32 +0000 | [diff] [blame] | 744 | if (ResolvedParamIndex == ParamCommandComment::VarArgParamIndex) { |
| 745 | PCC->setIsVarArgParam(); |
| 746 | continue; |
| 747 | } |
Dmitri Gribenko | 219bd15 | 2012-08-24 17:45:39 +0000 | [diff] [blame] | 748 | if (ResolvedParamIndex == ParamCommandComment::InvalidParamIndex) { |
| 749 | UnresolvedParamCommands.push_back(PCC); |
| 750 | continue; |
| 751 | } |
| 752 | PCC->setParamIndex(ResolvedParamIndex); |
| 753 | if (ParamVarDocs[ResolvedParamIndex]) { |
| 754 | SourceRange ArgRange = PCC->getParamNameRange(); |
| 755 | Diag(ArgRange.getBegin(), diag::warn_doc_param_duplicate) |
| 756 | << ParamName << ArgRange; |
| 757 | ParamCommandComment *PrevCommand = ParamVarDocs[ResolvedParamIndex]; |
| 758 | Diag(PrevCommand->getLocation(), diag::note_doc_param_previous) |
| 759 | << PrevCommand->getParamNameRange(); |
| 760 | } |
| 761 | ParamVarDocs[ResolvedParamIndex] = PCC; |
| 762 | } |
| 763 | |
| 764 | // Find parameter declarations that have no corresponding \\param. |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 765 | SmallVector<const ParmVarDecl *, 8> OrphanedParamDecls; |
Dmitri Gribenko | 219bd15 | 2012-08-24 17:45:39 +0000 | [diff] [blame] | 766 | for (unsigned i = 0, e = ParamVarDocs.size(); i != e; ++i) { |
| 767 | if (!ParamVarDocs[i]) |
| 768 | OrphanedParamDecls.push_back(ParamVars[i]); |
| 769 | } |
| 770 | |
| 771 | // Second pass over unresolved \\param commands: do typo correction. |
| 772 | // Suggest corrections from a set of parameter declarations that have no |
| 773 | // corresponding \\param. |
| 774 | for (unsigned i = 0, e = UnresolvedParamCommands.size(); i != e; ++i) { |
| 775 | const ParamCommandComment *PCC = UnresolvedParamCommands[i]; |
| 776 | |
| 777 | SourceRange ArgRange = PCC->getParamNameRange(); |
Fariborz Jahanian | 9d2f1e7 | 2012-10-18 21:42:42 +0000 | [diff] [blame] | 778 | StringRef ParamName = PCC->getParamNameAsWritten(); |
Dmitri Gribenko | 219bd15 | 2012-08-24 17:45:39 +0000 | [diff] [blame] | 779 | Diag(ArgRange.getBegin(), diag::warn_doc_param_not_found) |
| 780 | << ParamName << ArgRange; |
| 781 | |
| 782 | // All parameters documented -- can't suggest a correction. |
| 783 | if (OrphanedParamDecls.size() == 0) |
| 784 | continue; |
| 785 | |
| 786 | unsigned CorrectedParamIndex = ParamCommandComment::InvalidParamIndex; |
| 787 | if (OrphanedParamDecls.size() == 1) { |
| 788 | // If one parameter is not documented then that parameter is the only |
| 789 | // possible suggestion. |
| 790 | CorrectedParamIndex = 0; |
| 791 | } else { |
| 792 | // Do typo correction. |
| 793 | CorrectedParamIndex = correctTypoInParmVarReference(ParamName, |
| 794 | OrphanedParamDecls); |
| 795 | } |
| 796 | if (CorrectedParamIndex != ParamCommandComment::InvalidParamIndex) { |
| 797 | const ParmVarDecl *CorrectedPVD = OrphanedParamDecls[CorrectedParamIndex]; |
| 798 | if (const IdentifierInfo *CorrectedII = CorrectedPVD->getIdentifier()) |
| 799 | Diag(ArgRange.getBegin(), diag::note_doc_param_name_suggestion) |
| 800 | << CorrectedII->getName() |
| 801 | << FixItHint::CreateReplacement(ArgRange, CorrectedII->getName()); |
| 802 | } |
| 803 | } |
| 804 | } |
| 805 | |
Dmitri Gribenko | 4b7f5fe | 2012-07-23 17:40:30 +0000 | [diff] [blame] | 806 | bool Sema::isFunctionDecl() { |
Dmitri Gribenko | 527ab21 | 2012-08-01 23:08:09 +0000 | [diff] [blame] | 807 | if (!ThisDeclInfo) |
| 808 | return false; |
| 809 | if (!ThisDeclInfo->IsFilled) |
Dmitri Gribenko | 52cb218 | 2012-07-24 20:58:46 +0000 | [diff] [blame] | 810 | inspectThisDecl(); |
Dmitri Gribenko | 37a7faf | 2012-08-02 21:45:39 +0000 | [diff] [blame] | 811 | return ThisDeclInfo->getKind() == DeclInfo::FunctionKind; |
Dmitri Gribenko | 4b7f5fe | 2012-07-23 17:40:30 +0000 | [diff] [blame] | 812 | } |
Fariborz Jahanian | 56fe406 | 2013-03-05 22:46:07 +0000 | [diff] [blame] | 813 | |
Fariborz Jahanian | a668bf5 | 2013-03-05 23:20:29 +0000 | [diff] [blame] | 814 | bool Sema::isAnyFunctionDecl() { |
| 815 | return isFunctionDecl() && ThisDeclInfo->CurrentDecl && |
| 816 | isa<FunctionDecl>(ThisDeclInfo->CurrentDecl); |
| 817 | } |
Dmitri Gribenko | 02489eb | 2013-06-24 04:41:32 +0000 | [diff] [blame] | 818 | |
| 819 | bool Sema::isFunctionOrMethodVariadic() { |
Dmitri Gribenko | d9eb05a | 2014-03-19 13:59:36 +0000 | [diff] [blame] | 820 | if (!isAnyFunctionDecl() && !isObjCMethodDecl() && !isFunctionTemplateDecl()) |
Dmitri Gribenko | 02489eb | 2013-06-24 04:41:32 +0000 | [diff] [blame] | 821 | return false; |
| 822 | if (const FunctionDecl *FD = |
| 823 | dyn_cast<FunctionDecl>(ThisDeclInfo->CurrentDecl)) |
| 824 | return FD->isVariadic(); |
Dmitri Gribenko | d9eb05a | 2014-03-19 13:59:36 +0000 | [diff] [blame] | 825 | if (const FunctionTemplateDecl *FTD = |
| 826 | dyn_cast<FunctionTemplateDecl>(ThisDeclInfo->CurrentDecl)) |
| 827 | return FTD->getTemplatedDecl()->isVariadic(); |
Dmitri Gribenko | 02489eb | 2013-06-24 04:41:32 +0000 | [diff] [blame] | 828 | if (const ObjCMethodDecl *MD = |
| 829 | dyn_cast<ObjCMethodDecl>(ThisDeclInfo->CurrentDecl)) |
| 830 | return MD->isVariadic(); |
| 831 | return false; |
| 832 | } |
| 833 | |
Fariborz Jahanian | 56fe406 | 2013-03-05 22:46:07 +0000 | [diff] [blame] | 834 | bool Sema::isObjCMethodDecl() { |
| 835 | return isFunctionDecl() && ThisDeclInfo->CurrentDecl && |
| 836 | isa<ObjCMethodDecl>(ThisDeclInfo->CurrentDecl); |
| 837 | } |
Dmitri Gribenko | c0510b9 | 2013-06-24 01:33:34 +0000 | [diff] [blame] | 838 | |
Fariborz Jahanian | 56fe406 | 2013-03-05 22:46:07 +0000 | [diff] [blame] | 839 | bool Sema::isFunctionPointerVarDecl() { |
Fariborz Jahanian | f4ba35d | 2013-03-05 19:40:47 +0000 | [diff] [blame] | 840 | if (!ThisDeclInfo) |
| 841 | return false; |
| 842 | if (!ThisDeclInfo->IsFilled) |
| 843 | inspectThisDecl(); |
| 844 | if (ThisDeclInfo->getKind() == DeclInfo::VariableKind) { |
| 845 | if (const VarDecl *VD = dyn_cast_or_null<VarDecl>(ThisDeclInfo->CurrentDecl)) { |
| 846 | QualType QT = VD->getType(); |
| 847 | return QT->isFunctionPointerType(); |
| 848 | } |
| 849 | } |
| 850 | return false; |
| 851 | } |
Dmitri Gribenko | 110dfa8 | 2013-12-17 19:28:18 +0000 | [diff] [blame] | 852 | |
Fariborz Jahanian | 81bbee1 | 2013-02-27 00:46:06 +0000 | [diff] [blame] | 853 | bool Sema::isObjCPropertyDecl() { |
| 854 | if (!ThisDeclInfo) |
| 855 | return false; |
| 856 | if (!ThisDeclInfo->IsFilled) |
| 857 | inspectThisDecl(); |
| 858 | return ThisDeclInfo->CurrentDecl->getKind() == Decl::ObjCProperty; |
| 859 | } |
Dmitri Gribenko | 4b7f5fe | 2012-07-23 17:40:30 +0000 | [diff] [blame] | 860 | |
Dmitri Gribenko | 8e5d5f1 | 2012-08-06 21:31:15 +0000 | [diff] [blame] | 861 | bool Sema::isTemplateOrSpecialization() { |
Dmitri Gribenko | 527ab21 | 2012-08-01 23:08:09 +0000 | [diff] [blame] | 862 | if (!ThisDeclInfo) |
| 863 | return false; |
| 864 | if (!ThisDeclInfo->IsFilled) |
Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 865 | inspectThisDecl(); |
Dmitri Gribenko | 8e5d5f1 | 2012-08-06 21:31:15 +0000 | [diff] [blame] | 866 | return ThisDeclInfo->getTemplateKind() != DeclInfo::NotTemplate; |
Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 867 | } |
| 868 | |
Fariborz Jahanian | cfbac5d | 2013-03-08 23:59:23 +0000 | [diff] [blame] | 869 | bool Sema::isRecordLikeDecl() { |
Fariborz Jahanian | a649eee | 2013-03-07 23:33:11 +0000 | [diff] [blame] | 870 | if (!ThisDeclInfo) |
| 871 | return false; |
| 872 | if (!ThisDeclInfo->IsFilled) |
| 873 | inspectThisDecl(); |
Dmitri Gribenko | 110dfa8 | 2013-12-17 19:28:18 +0000 | [diff] [blame] | 874 | return isUnionDecl() || isClassOrStructDecl() || isObjCInterfaceDecl() || |
| 875 | isObjCProtocolDecl(); |
Fariborz Jahanian | a649eee | 2013-03-07 23:33:11 +0000 | [diff] [blame] | 876 | } |
| 877 | |
| 878 | bool Sema::isUnionDecl() { |
| 879 | if (!ThisDeclInfo) |
| 880 | return false; |
| 881 | if (!ThisDeclInfo->IsFilled) |
| 882 | inspectThisDecl(); |
| 883 | if (const RecordDecl *RD = |
| 884 | dyn_cast_or_null<RecordDecl>(ThisDeclInfo->CurrentDecl)) |
| 885 | return RD->isUnion(); |
| 886 | return false; |
| 887 | } |
Dmitri Gribenko | 110dfa8 | 2013-12-17 19:28:18 +0000 | [diff] [blame] | 888 | |
Fariborz Jahanian | cfbac5d | 2013-03-08 23:59:23 +0000 | [diff] [blame] | 889 | bool Sema::isClassOrStructDecl() { |
Fariborz Jahanian | a649eee | 2013-03-07 23:33:11 +0000 | [diff] [blame] | 890 | if (!ThisDeclInfo) |
| 891 | return false; |
| 892 | if (!ThisDeclInfo->IsFilled) |
| 893 | inspectThisDecl(); |
| 894 | return ThisDeclInfo->CurrentDecl && |
| 895 | isa<RecordDecl>(ThisDeclInfo->CurrentDecl) && |
| 896 | !isUnionDecl(); |
| 897 | } |
Dmitri Gribenko | 110dfa8 | 2013-12-17 19:28:18 +0000 | [diff] [blame] | 898 | |
Fariborz Jahanian | c0607ed | 2013-06-19 18:08:03 +0000 | [diff] [blame] | 899 | bool Sema::isClassTemplateDecl() { |
| 900 | if (!ThisDeclInfo) |
| 901 | return false; |
| 902 | if (!ThisDeclInfo->IsFilled) |
| 903 | inspectThisDecl(); |
| 904 | return ThisDeclInfo->CurrentDecl && |
| 905 | (isa<ClassTemplateDecl>(ThisDeclInfo->CurrentDecl)); |
| 906 | } |
| 907 | |
| 908 | bool Sema::isFunctionTemplateDecl() { |
| 909 | if (!ThisDeclInfo) |
| 910 | return false; |
| 911 | if (!ThisDeclInfo->IsFilled) |
| 912 | inspectThisDecl(); |
| 913 | return ThisDeclInfo->CurrentDecl && |
Dmitri Gribenko | 110dfa8 | 2013-12-17 19:28:18 +0000 | [diff] [blame] | 914 | (isa<FunctionTemplateDecl>(ThisDeclInfo->CurrentDecl)); |
Fariborz Jahanian | c0607ed | 2013-06-19 18:08:03 +0000 | [diff] [blame] | 915 | } |
Fariborz Jahanian | a649eee | 2013-03-07 23:33:11 +0000 | [diff] [blame] | 916 | |
| 917 | bool Sema::isObjCInterfaceDecl() { |
| 918 | if (!ThisDeclInfo) |
| 919 | return false; |
| 920 | if (!ThisDeclInfo->IsFilled) |
| 921 | inspectThisDecl(); |
| 922 | return ThisDeclInfo->CurrentDecl && |
| 923 | isa<ObjCInterfaceDecl>(ThisDeclInfo->CurrentDecl); |
| 924 | } |
Dmitri Gribenko | 110dfa8 | 2013-12-17 19:28:18 +0000 | [diff] [blame] | 925 | |
Fariborz Jahanian | a649eee | 2013-03-07 23:33:11 +0000 | [diff] [blame] | 926 | bool Sema::isObjCProtocolDecl() { |
| 927 | if (!ThisDeclInfo) |
| 928 | return false; |
| 929 | if (!ThisDeclInfo->IsFilled) |
| 930 | inspectThisDecl(); |
| 931 | return ThisDeclInfo->CurrentDecl && |
| 932 | isa<ObjCProtocolDecl>(ThisDeclInfo->CurrentDecl); |
| 933 | } |
Dmitri Gribenko | 110dfa8 | 2013-12-17 19:28:18 +0000 | [diff] [blame] | 934 | |
Dmitri Gribenko | 4b7f5fe | 2012-07-23 17:40:30 +0000 | [diff] [blame] | 935 | ArrayRef<const ParmVarDecl *> Sema::getParamVars() { |
Dmitri Gribenko | 527ab21 | 2012-08-01 23:08:09 +0000 | [diff] [blame] | 936 | if (!ThisDeclInfo->IsFilled) |
Dmitri Gribenko | 52cb218 | 2012-07-24 20:58:46 +0000 | [diff] [blame] | 937 | inspectThisDecl(); |
Dmitri Gribenko | 527ab21 | 2012-08-01 23:08:09 +0000 | [diff] [blame] | 938 | return ThisDeclInfo->ParamVars; |
Dmitri Gribenko | 4b7f5fe | 2012-07-23 17:40:30 +0000 | [diff] [blame] | 939 | } |
| 940 | |
| 941 | void Sema::inspectThisDecl() { |
Dmitri Gribenko | 527ab21 | 2012-08-01 23:08:09 +0000 | [diff] [blame] | 942 | ThisDeclInfo->fill(); |
Dmitri Gribenko | 4b7f5fe | 2012-07-23 17:40:30 +0000 | [diff] [blame] | 943 | } |
| 944 | |
Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 945 | unsigned Sema::resolveParmVarReference(StringRef Name, |
Dmitri Gribenko | 4b7f5fe | 2012-07-23 17:40:30 +0000 | [diff] [blame] | 946 | ArrayRef<const ParmVarDecl *> ParamVars) { |
| 947 | for (unsigned i = 0, e = ParamVars.size(); i != e; ++i) { |
Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 948 | const IdentifierInfo *II = ParamVars[i]->getIdentifier(); |
| 949 | if (II && II->getName() == Name) |
| 950 | return i; |
| 951 | } |
Dmitri Gribenko | 02489eb | 2013-06-24 04:41:32 +0000 | [diff] [blame] | 952 | if (Name == "..." && isFunctionOrMethodVariadic()) |
| 953 | return ParamCommandComment::VarArgParamIndex; |
Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 954 | return ParamCommandComment::InvalidParamIndex; |
| 955 | } |
| 956 | |
Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 957 | namespace { |
| 958 | class SimpleTypoCorrector { |
| 959 | StringRef Typo; |
| 960 | const unsigned MaxEditDistance; |
| 961 | |
| 962 | const NamedDecl *BestDecl; |
| 963 | unsigned BestEditDistance; |
| 964 | unsigned BestIndex; |
| 965 | unsigned NextIndex; |
| 966 | |
| 967 | public: |
| 968 | SimpleTypoCorrector(StringRef Typo) : |
| 969 | Typo(Typo), MaxEditDistance((Typo.size() + 2) / 3), |
| 970 | BestDecl(NULL), BestEditDistance(MaxEditDistance + 1), |
| 971 | BestIndex(0), NextIndex(0) |
| 972 | { } |
| 973 | |
| 974 | void addDecl(const NamedDecl *ND); |
| 975 | |
| 976 | const NamedDecl *getBestDecl() const { |
| 977 | if (BestEditDistance > MaxEditDistance) |
| 978 | return NULL; |
| 979 | |
| 980 | return BestDecl; |
| 981 | } |
| 982 | |
| 983 | unsigned getBestDeclIndex() const { |
| 984 | assert(getBestDecl()); |
| 985 | return BestIndex; |
| 986 | } |
| 987 | }; |
| 988 | |
| 989 | void SimpleTypoCorrector::addDecl(const NamedDecl *ND) { |
| 990 | unsigned CurrIndex = NextIndex++; |
| 991 | |
| 992 | const IdentifierInfo *II = ND->getIdentifier(); |
| 993 | if (!II) |
| 994 | return; |
| 995 | |
| 996 | StringRef Name = II->getName(); |
| 997 | unsigned MinPossibleEditDistance = abs((int)Name.size() - (int)Typo.size()); |
| 998 | if (MinPossibleEditDistance > 0 && |
| 999 | Typo.size() / MinPossibleEditDistance < 3) |
| 1000 | return; |
| 1001 | |
| 1002 | unsigned EditDistance = Typo.edit_distance(Name, true, MaxEditDistance); |
| 1003 | if (EditDistance < BestEditDistance) { |
| 1004 | BestEditDistance = EditDistance; |
| 1005 | BestDecl = ND; |
| 1006 | BestIndex = CurrIndex; |
| 1007 | } |
| 1008 | } |
| 1009 | } // unnamed namespace |
| 1010 | |
Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 1011 | unsigned Sema::correctTypoInParmVarReference( |
| 1012 | StringRef Typo, |
Dmitri Gribenko | 4b7f5fe | 2012-07-23 17:40:30 +0000 | [diff] [blame] | 1013 | ArrayRef<const ParmVarDecl *> ParamVars) { |
Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 1014 | SimpleTypoCorrector Corrector(Typo); |
| 1015 | for (unsigned i = 0, e = ParamVars.size(); i != e; ++i) |
| 1016 | Corrector.addDecl(ParamVars[i]); |
| 1017 | if (Corrector.getBestDecl()) |
| 1018 | return Corrector.getBestDeclIndex(); |
| 1019 | else |
Dmitri Gribenko | 76bb5cabfa | 2012-09-10 21:20:09 +0000 | [diff] [blame] | 1020 | return ParamCommandComment::InvalidParamIndex; |
Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 1021 | } |
Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 1022 | |
Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 1023 | namespace { |
| 1024 | bool ResolveTParamReferenceHelper( |
| 1025 | StringRef Name, |
| 1026 | const TemplateParameterList *TemplateParameters, |
| 1027 | SmallVectorImpl<unsigned> *Position) { |
| 1028 | for (unsigned i = 0, e = TemplateParameters->size(); i != e; ++i) { |
| 1029 | const NamedDecl *Param = TemplateParameters->getParam(i); |
| 1030 | const IdentifierInfo *II = Param->getIdentifier(); |
| 1031 | if (II && II->getName() == Name) { |
| 1032 | Position->push_back(i); |
| 1033 | return true; |
| 1034 | } |
| 1035 | |
| 1036 | if (const TemplateTemplateParmDecl *TTP = |
| 1037 | dyn_cast<TemplateTemplateParmDecl>(Param)) { |
| 1038 | Position->push_back(i); |
| 1039 | if (ResolveTParamReferenceHelper(Name, TTP->getTemplateParameters(), |
| 1040 | Position)) |
| 1041 | return true; |
| 1042 | Position->pop_back(); |
Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 1043 | } |
| 1044 | } |
Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 1045 | return false; |
| 1046 | } |
| 1047 | } // unnamed namespace |
Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 1048 | |
Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 1049 | bool Sema::resolveTParamReference( |
| 1050 | StringRef Name, |
| 1051 | const TemplateParameterList *TemplateParameters, |
| 1052 | SmallVectorImpl<unsigned> *Position) { |
| 1053 | Position->clear(); |
| 1054 | if (!TemplateParameters) |
| 1055 | return false; |
| 1056 | |
| 1057 | return ResolveTParamReferenceHelper(Name, TemplateParameters, Position); |
| 1058 | } |
| 1059 | |
| 1060 | namespace { |
| 1061 | void CorrectTypoInTParamReferenceHelper( |
| 1062 | const TemplateParameterList *TemplateParameters, |
| 1063 | SimpleTypoCorrector &Corrector) { |
| 1064 | for (unsigned i = 0, e = TemplateParameters->size(); i != e; ++i) { |
| 1065 | const NamedDecl *Param = TemplateParameters->getParam(i); |
| 1066 | Corrector.addDecl(Param); |
| 1067 | |
| 1068 | if (const TemplateTemplateParmDecl *TTP = |
| 1069 | dyn_cast<TemplateTemplateParmDecl>(Param)) |
| 1070 | CorrectTypoInTParamReferenceHelper(TTP->getTemplateParameters(), |
| 1071 | Corrector); |
| 1072 | } |
| 1073 | } |
| 1074 | } // unnamed namespace |
| 1075 | |
| 1076 | StringRef Sema::correctTypoInTParamReference( |
| 1077 | StringRef Typo, |
| 1078 | const TemplateParameterList *TemplateParameters) { |
| 1079 | SimpleTypoCorrector Corrector(Typo); |
| 1080 | CorrectTypoInTParamReferenceHelper(TemplateParameters, Corrector); |
| 1081 | if (const NamedDecl *ND = Corrector.getBestDecl()) { |
| 1082 | const IdentifierInfo *II = ND->getIdentifier(); |
| 1083 | assert(II && "SimpleTypoCorrector should not return this decl"); |
| 1084 | return II->getName(); |
| 1085 | } |
| 1086 | return StringRef(); |
Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 1087 | } |
| 1088 | |
Dmitri Gribenko | d73e4ce | 2012-07-23 16:43:01 +0000 | [diff] [blame] | 1089 | InlineCommandComment::RenderKind |
| 1090 | Sema::getInlineCommandRenderKind(StringRef Name) const { |
Dmitri Gribenko | 7acbf00 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 1091 | assert(Traits.getCommandInfo(Name)->IsInlineCommand); |
Dmitri Gribenko | d73e4ce | 2012-07-23 16:43:01 +0000 | [diff] [blame] | 1092 | |
| 1093 | return llvm::StringSwitch<InlineCommandComment::RenderKind>(Name) |
| 1094 | .Case("b", InlineCommandComment::RenderBold) |
| 1095 | .Cases("c", "p", InlineCommandComment::RenderMonospaced) |
| 1096 | .Cases("a", "e", "em", InlineCommandComment::RenderEmphasized) |
| 1097 | .Default(InlineCommandComment::RenderNormal); |
| 1098 | } |
| 1099 | |
Dmitri Gribenko | ec92531 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 1100 | } // end namespace comments |
| 1101 | } // end namespace clang |
| 1102 | |