Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 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" |
Benjamin Kramer | 2fa67ef | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 11 | #include "clang/AST/Attr.h" |
Dmitri Gribenko | aa58081 | 2012-08-09 00:03:17 +0000 | [diff] [blame] | 12 | #include "clang/AST/CommentCommandTraits.h" |
Benjamin Kramer | 2fa67ef | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 13 | #include "clang/AST/CommentDiagnostic.h" |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 14 | #include "clang/AST/Decl.h" |
Dmitri Gribenko | 96b0986 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 15 | #include "clang/AST/DeclTemplate.h" |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 16 | #include "clang/Basic/SourceManager.h" |
Dmitri Gribenko | 1952354 | 2012-09-29 11:40:46 +0000 | [diff] [blame] | 17 | #include "clang/Lex/Preprocessor.h" |
Dmitri Gribenko | 1952354 | 2012-09-29 11:40:46 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallString.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/StringSwitch.h" |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 20 | |
| 21 | namespace clang { |
| 22 | namespace comments { |
| 23 | |
Dmitri Gribenko | c24a76e | 2012-08-31 02:21:44 +0000 | [diff] [blame] | 24 | namespace { |
| 25 | #include "clang/AST/CommentHTMLTagsProperties.inc" |
| 26 | } // unnamed namespace |
| 27 | |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 28 | Sema::Sema(llvm::BumpPtrAllocator &Allocator, const SourceManager &SourceMgr, |
Dmitri Gribenko | 1952354 | 2012-09-29 11:40:46 +0000 | [diff] [blame] | 29 | DiagnosticsEngine &Diags, CommandTraits &Traits, |
| 30 | const Preprocessor *PP) : |
Dmitri Gribenko | aa58081 | 2012-08-09 00:03:17 +0000 | [diff] [blame] | 31 | Allocator(Allocator), SourceMgr(SourceMgr), Diags(Diags), Traits(Traits), |
Fariborz Jahanian | f843a58 | 2013-01-31 23:12:39 +0000 | [diff] [blame] | 32 | PP(PP), ThisDeclInfo(NULL), BriefCommand(NULL), ReturnsCommand(NULL), |
| 33 | HeaderfileCommand(NULL) { |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | void Sema::setDecl(const Decl *D) { |
Dmitri Gribenko | 1ca7ecc | 2012-08-01 23:08:09 +0000 | [diff] [blame] | 37 | if (!D) |
| 38 | return; |
| 39 | |
| 40 | ThisDeclInfo = new (Allocator) DeclInfo; |
Fariborz Jahanian | bf967be | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 41 | ThisDeclInfo->CommentDecl = D; |
Dmitri Gribenko | 651f8ce | 2012-08-01 23:21:57 +0000 | [diff] [blame] | 42 | ThisDeclInfo->IsFilled = false; |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | ParagraphComment *Sema::actOnParagraphComment( |
| 46 | ArrayRef<InlineContentComment *> Content) { |
| 47 | return new (Allocator) ParagraphComment(Content); |
| 48 | } |
| 49 | |
Dmitri Gribenko | 808383d | 2013-03-04 23:06:15 +0000 | [diff] [blame] | 50 | BlockCommandComment *Sema::actOnBlockCommandStart( |
| 51 | SourceLocation LocBegin, |
| 52 | SourceLocation LocEnd, |
| 53 | unsigned CommandID, |
| 54 | CommandMarkerKind CommandMarker) { |
| 55 | return new (Allocator) BlockCommandComment(LocBegin, LocEnd, CommandID, |
| 56 | CommandMarker); |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Dmitri Gribenko | 7d9b511 | 2012-08-06 19:03:12 +0000 | [diff] [blame] | 59 | void Sema::actOnBlockCommandArgs(BlockCommandComment *Command, |
| 60 | ArrayRef<BlockCommandComment::Argument> Args) { |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 61 | Command->setArgs(Args); |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Dmitri Gribenko | 7d9b511 | 2012-08-06 19:03:12 +0000 | [diff] [blame] | 64 | void Sema::actOnBlockCommandFinish(BlockCommandComment *Command, |
| 65 | ParagraphComment *Paragraph) { |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 66 | Command->setParagraph(Paragraph); |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 67 | checkBlockCommandEmptyParagraph(Command); |
Dmitri Gribenko | 9443c57 | 2012-08-06 17:08:27 +0000 | [diff] [blame] | 68 | checkBlockCommandDuplicate(Command); |
Dmitri Gribenko | 89ab7d0 | 2012-08-03 21:15:32 +0000 | [diff] [blame] | 69 | checkReturnsCommand(Command); |
Dmitri Gribenko | 0bd9838 | 2012-09-22 21:47:50 +0000 | [diff] [blame] | 70 | checkDeprecatedCommand(Command); |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Dmitri Gribenko | 808383d | 2013-03-04 23:06:15 +0000 | [diff] [blame] | 73 | ParamCommandComment *Sema::actOnParamCommandStart( |
| 74 | SourceLocation LocBegin, |
| 75 | SourceLocation LocEnd, |
| 76 | unsigned CommandID, |
| 77 | CommandMarkerKind CommandMarker) { |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 78 | ParamCommandComment *Command = |
Dmitri Gribenko | 808383d | 2013-03-04 23:06:15 +0000 | [diff] [blame] | 79 | new (Allocator) ParamCommandComment(LocBegin, LocEnd, CommandID, |
| 80 | CommandMarker); |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 81 | |
Dmitri Gribenko | 8487c52 | 2012-07-23 17:40:30 +0000 | [diff] [blame] | 82 | if (!isFunctionDecl()) |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 83 | Diag(Command->getLocation(), |
| 84 | diag::warn_doc_param_not_attached_to_a_function_decl) |
Dmitri Gribenko | 808383d | 2013-03-04 23:06:15 +0000 | [diff] [blame] | 85 | << CommandMarker |
Dmitri Gribenko | e4330a3 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 86 | << Command->getCommandNameRange(Traits); |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 87 | |
| 88 | return Command; |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Dmitri Gribenko | 7d9b511 | 2012-08-06 19:03:12 +0000 | [diff] [blame] | 91 | void Sema::actOnParamCommandDirectionArg(ParamCommandComment *Command, |
| 92 | SourceLocation ArgLocBegin, |
| 93 | SourceLocation ArgLocEnd, |
| 94 | StringRef Arg) { |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 95 | ParamCommandComment::PassDirection Direction; |
| 96 | std::string ArgLower = Arg.lower(); |
| 97 | // TODO: optimize: lower Name first (need an API in SmallString for that), |
| 98 | // after that StringSwitch. |
| 99 | if (ArgLower == "[in]") |
| 100 | Direction = ParamCommandComment::In; |
| 101 | else if (ArgLower == "[out]") |
| 102 | Direction = ParamCommandComment::Out; |
| 103 | else if (ArgLower == "[in,out]" || ArgLower == "[out,in]") |
| 104 | Direction = ParamCommandComment::InOut; |
| 105 | else { |
| 106 | // Remove spaces. |
| 107 | std::string::iterator O = ArgLower.begin(); |
| 108 | for (std::string::iterator I = ArgLower.begin(), E = ArgLower.end(); |
| 109 | I != E; ++I) { |
| 110 | const char C = *I; |
| 111 | if (C != ' ' && C != '\n' && C != '\r' && |
| 112 | C != '\t' && C != '\v' && C != '\f') |
| 113 | *O++ = C; |
| 114 | } |
| 115 | ArgLower.resize(O - ArgLower.begin()); |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 116 | |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 117 | bool RemovingWhitespaceHelped = false; |
| 118 | if (ArgLower == "[in]") { |
| 119 | Direction = ParamCommandComment::In; |
| 120 | RemovingWhitespaceHelped = true; |
| 121 | } else if (ArgLower == "[out]") { |
| 122 | Direction = ParamCommandComment::Out; |
| 123 | RemovingWhitespaceHelped = true; |
| 124 | } else if (ArgLower == "[in,out]" || ArgLower == "[out,in]") { |
| 125 | Direction = ParamCommandComment::InOut; |
| 126 | RemovingWhitespaceHelped = true; |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 127 | } else { |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 128 | Direction = ParamCommandComment::In; |
| 129 | RemovingWhitespaceHelped = false; |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 130 | } |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 131 | |
| 132 | SourceRange ArgRange(ArgLocBegin, ArgLocEnd); |
| 133 | if (RemovingWhitespaceHelped) |
| 134 | Diag(ArgLocBegin, diag::warn_doc_param_spaces_in_direction) |
| 135 | << ArgRange |
| 136 | << FixItHint::CreateReplacement( |
| 137 | ArgRange, |
| 138 | ParamCommandComment::getDirectionAsString(Direction)); |
| 139 | else |
| 140 | Diag(ArgLocBegin, diag::warn_doc_param_invalid_direction) |
| 141 | << ArgRange; |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 142 | } |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 143 | Command->setDirection(Direction, /* Explicit = */ true); |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Dmitri Gribenko | 7d9b511 | 2012-08-06 19:03:12 +0000 | [diff] [blame] | 146 | void Sema::actOnParamCommandParamNameArg(ParamCommandComment *Command, |
| 147 | SourceLocation ArgLocBegin, |
| 148 | SourceLocation ArgLocEnd, |
| 149 | StringRef Arg) { |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 150 | // Parser will not feed us more arguments than needed. |
Dmitri Gribenko | 0eaf69d | 2012-07-13 19:02:42 +0000 | [diff] [blame] | 151 | assert(Command->getNumArgs() == 0); |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 152 | |
| 153 | if (!Command->isDirectionExplicit()) { |
| 154 | // User didn't provide a direction argument. |
| 155 | Command->setDirection(ParamCommandComment::In, /* Explicit = */ false); |
| 156 | } |
| 157 | typedef BlockCommandComment::Argument Argument; |
| 158 | Argument *A = new (Allocator) Argument(SourceRange(ArgLocBegin, |
| 159 | ArgLocEnd), |
| 160 | Arg); |
| 161 | Command->setArgs(llvm::makeArrayRef(A, 1)); |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Dmitri Gribenko | 7d9b511 | 2012-08-06 19:03:12 +0000 | [diff] [blame] | 164 | void Sema::actOnParamCommandFinish(ParamCommandComment *Command, |
| 165 | ParagraphComment *Paragraph) { |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 166 | Command->setParagraph(Paragraph); |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 167 | checkBlockCommandEmptyParagraph(Command); |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Dmitri Gribenko | 808383d | 2013-03-04 23:06:15 +0000 | [diff] [blame] | 170 | TParamCommandComment *Sema::actOnTParamCommandStart( |
| 171 | SourceLocation LocBegin, |
| 172 | SourceLocation LocEnd, |
| 173 | unsigned CommandID, |
| 174 | CommandMarkerKind CommandMarker) { |
Dmitri Gribenko | 96b0986 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 175 | TParamCommandComment *Command = |
Dmitri Gribenko | 808383d | 2013-03-04 23:06:15 +0000 | [diff] [blame] | 176 | new (Allocator) TParamCommandComment(LocBegin, LocEnd, CommandID, |
| 177 | CommandMarker); |
Dmitri Gribenko | 96b0986 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 178 | |
Dmitri Gribenko | 04bf29e | 2012-08-06 21:31:15 +0000 | [diff] [blame] | 179 | if (!isTemplateOrSpecialization()) |
Dmitri Gribenko | 96b0986 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 180 | Diag(Command->getLocation(), |
| 181 | diag::warn_doc_tparam_not_attached_to_a_template_decl) |
Dmitri Gribenko | 808383d | 2013-03-04 23:06:15 +0000 | [diff] [blame] | 182 | << CommandMarker |
Dmitri Gribenko | e4330a3 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 183 | << Command->getCommandNameRange(Traits); |
Dmitri Gribenko | 96b0986 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 184 | |
| 185 | return Command; |
| 186 | } |
| 187 | |
Dmitri Gribenko | 7d9b511 | 2012-08-06 19:03:12 +0000 | [diff] [blame] | 188 | void Sema::actOnTParamCommandParamNameArg(TParamCommandComment *Command, |
| 189 | SourceLocation ArgLocBegin, |
| 190 | SourceLocation ArgLocEnd, |
| 191 | StringRef Arg) { |
Dmitri Gribenko | 96b0986 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 192 | // Parser will not feed us more arguments than needed. |
| 193 | assert(Command->getNumArgs() == 0); |
| 194 | |
| 195 | typedef BlockCommandComment::Argument Argument; |
| 196 | Argument *A = new (Allocator) Argument(SourceRange(ArgLocBegin, |
| 197 | ArgLocEnd), |
| 198 | Arg); |
| 199 | Command->setArgs(llvm::makeArrayRef(A, 1)); |
| 200 | |
Dmitri Gribenko | 04bf29e | 2012-08-06 21:31:15 +0000 | [diff] [blame] | 201 | if (!isTemplateOrSpecialization()) { |
Dmitri Gribenko | 96b0986 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 202 | // We already warned that this \\tparam is not attached to a template decl. |
Dmitri Gribenko | 7d9b511 | 2012-08-06 19:03:12 +0000 | [diff] [blame] | 203 | return; |
Dmitri Gribenko | 96b0986 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Dmitri Gribenko | 1ca7ecc | 2012-08-01 23:08:09 +0000 | [diff] [blame] | 206 | const TemplateParameterList *TemplateParameters = |
| 207 | ThisDeclInfo->TemplateParameters; |
Dmitri Gribenko | 96b0986 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 208 | SmallVector<unsigned, 2> Position; |
| 209 | if (resolveTParamReference(Arg, TemplateParameters, &Position)) { |
| 210 | Command->setPosition(copyArray(llvm::makeArrayRef(Position))); |
| 211 | llvm::StringMap<TParamCommandComment *>::iterator PrevCommandIt = |
| 212 | TemplateParameterDocs.find(Arg); |
| 213 | if (PrevCommandIt != TemplateParameterDocs.end()) { |
| 214 | SourceRange ArgRange(ArgLocBegin, ArgLocEnd); |
| 215 | Diag(ArgLocBegin, diag::warn_doc_tparam_duplicate) |
| 216 | << Arg << ArgRange; |
| 217 | TParamCommandComment *PrevCommand = PrevCommandIt->second; |
| 218 | Diag(PrevCommand->getLocation(), diag::note_doc_tparam_previous) |
| 219 | << PrevCommand->getParamNameRange(); |
| 220 | } |
| 221 | TemplateParameterDocs[Arg] = Command; |
Dmitri Gribenko | 7d9b511 | 2012-08-06 19:03:12 +0000 | [diff] [blame] | 222 | return; |
Dmitri Gribenko | 96b0986 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | SourceRange ArgRange(ArgLocBegin, ArgLocEnd); |
| 226 | Diag(ArgLocBegin, diag::warn_doc_tparam_not_found) |
| 227 | << Arg << ArgRange; |
| 228 | |
| 229 | if (!TemplateParameters || TemplateParameters->size() == 0) |
Dmitri Gribenko | 7d9b511 | 2012-08-06 19:03:12 +0000 | [diff] [blame] | 230 | return; |
Dmitri Gribenko | 96b0986 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 231 | |
| 232 | StringRef CorrectedName; |
| 233 | if (TemplateParameters->size() == 1) { |
| 234 | const NamedDecl *Param = TemplateParameters->getParam(0); |
| 235 | const IdentifierInfo *II = Param->getIdentifier(); |
| 236 | if (II) |
| 237 | CorrectedName = II->getName(); |
| 238 | } else { |
| 239 | CorrectedName = correctTypoInTParamReference(Arg, TemplateParameters); |
| 240 | } |
| 241 | |
| 242 | if (!CorrectedName.empty()) { |
| 243 | Diag(ArgLocBegin, diag::note_doc_tparam_name_suggestion) |
| 244 | << CorrectedName |
| 245 | << FixItHint::CreateReplacement(ArgRange, CorrectedName); |
| 246 | } |
| 247 | |
Dmitri Gribenko | 7d9b511 | 2012-08-06 19:03:12 +0000 | [diff] [blame] | 248 | return; |
Dmitri Gribenko | 96b0986 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Dmitri Gribenko | 7d9b511 | 2012-08-06 19:03:12 +0000 | [diff] [blame] | 251 | void Sema::actOnTParamCommandFinish(TParamCommandComment *Command, |
| 252 | ParagraphComment *Paragraph) { |
Dmitri Gribenko | 96b0986 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 253 | Command->setParagraph(Paragraph); |
| 254 | checkBlockCommandEmptyParagraph(Command); |
Dmitri Gribenko | 96b0986 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 255 | } |
| 256 | |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 257 | InlineCommandComment *Sema::actOnInlineCommand(SourceLocation CommandLocBegin, |
| 258 | SourceLocation CommandLocEnd, |
Dmitri Gribenko | e4330a3 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 259 | unsigned CommandID) { |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 260 | ArrayRef<InlineCommandComment::Argument> Args; |
Dmitri Gribenko | e4330a3 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 261 | StringRef CommandName = Traits.getCommandInfo(CommandID)->Name; |
Dmitri Gribenko | 2d66a50 | 2012-07-23 16:43:01 +0000 | [diff] [blame] | 262 | return new (Allocator) InlineCommandComment( |
| 263 | CommandLocBegin, |
| 264 | CommandLocEnd, |
Dmitri Gribenko | e4330a3 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 265 | CommandID, |
Dmitri Gribenko | 2d66a50 | 2012-07-23 16:43:01 +0000 | [diff] [blame] | 266 | getInlineCommandRenderKind(CommandName), |
| 267 | Args); |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | InlineCommandComment *Sema::actOnInlineCommand(SourceLocation CommandLocBegin, |
| 271 | SourceLocation CommandLocEnd, |
Dmitri Gribenko | e4330a3 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 272 | unsigned CommandID, |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 273 | SourceLocation ArgLocBegin, |
| 274 | SourceLocation ArgLocEnd, |
| 275 | StringRef Arg) { |
| 276 | typedef InlineCommandComment::Argument Argument; |
| 277 | Argument *A = new (Allocator) Argument(SourceRange(ArgLocBegin, |
| 278 | ArgLocEnd), |
| 279 | Arg); |
Dmitri Gribenko | e4330a3 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 280 | StringRef CommandName = Traits.getCommandInfo(CommandID)->Name; |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 281 | |
Dmitri Gribenko | 2d66a50 | 2012-07-23 16:43:01 +0000 | [diff] [blame] | 282 | return new (Allocator) InlineCommandComment( |
| 283 | CommandLocBegin, |
| 284 | CommandLocEnd, |
Dmitri Gribenko | e4330a3 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 285 | CommandID, |
Dmitri Gribenko | 2d66a50 | 2012-07-23 16:43:01 +0000 | [diff] [blame] | 286 | getInlineCommandRenderKind(CommandName), |
| 287 | llvm::makeArrayRef(A, 1)); |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | InlineContentComment *Sema::actOnUnknownCommand(SourceLocation LocBegin, |
| 291 | SourceLocation LocEnd, |
Dmitri Gribenko | b0b8a96 | 2012-09-11 19:22:03 +0000 | [diff] [blame] | 292 | StringRef CommandName) { |
| 293 | unsigned CommandID = Traits.registerUnknownCommand(CommandName)->getID(); |
| 294 | return actOnUnknownCommand(LocBegin, LocEnd, CommandID); |
| 295 | } |
| 296 | |
| 297 | InlineContentComment *Sema::actOnUnknownCommand(SourceLocation LocBegin, |
| 298 | SourceLocation LocEnd, |
| 299 | unsigned CommandID) { |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 300 | ArrayRef<InlineCommandComment::Argument> Args; |
Dmitri Gribenko | 2d66a50 | 2012-07-23 16:43:01 +0000 | [diff] [blame] | 301 | return new (Allocator) InlineCommandComment( |
Dmitri Gribenko | e4330a3 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 302 | LocBegin, LocEnd, CommandID, |
Dmitri Gribenko | 2d66a50 | 2012-07-23 16:43:01 +0000 | [diff] [blame] | 303 | InlineCommandComment::RenderNormal, |
| 304 | Args); |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | TextComment *Sema::actOnText(SourceLocation LocBegin, |
| 308 | SourceLocation LocEnd, |
| 309 | StringRef Text) { |
| 310 | return new (Allocator) TextComment(LocBegin, LocEnd, Text); |
| 311 | } |
| 312 | |
| 313 | VerbatimBlockComment *Sema::actOnVerbatimBlockStart(SourceLocation Loc, |
Dmitri Gribenko | e4330a3 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 314 | unsigned CommandID) { |
| 315 | StringRef CommandName = Traits.getCommandInfo(CommandID)->Name; |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 316 | return new (Allocator) VerbatimBlockComment( |
| 317 | Loc, |
Dmitri Gribenko | e4330a3 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 318 | Loc.getLocWithOffset(1 + CommandName.size()), |
| 319 | CommandID); |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | VerbatimBlockLineComment *Sema::actOnVerbatimBlockLine(SourceLocation Loc, |
| 323 | StringRef Text) { |
| 324 | return new (Allocator) VerbatimBlockLineComment(Loc, Text); |
| 325 | } |
| 326 | |
Dmitri Gribenko | 7d9b511 | 2012-08-06 19:03:12 +0000 | [diff] [blame] | 327 | void Sema::actOnVerbatimBlockFinish( |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 328 | VerbatimBlockComment *Block, |
| 329 | SourceLocation CloseNameLocBegin, |
| 330 | StringRef CloseName, |
| 331 | ArrayRef<VerbatimBlockLineComment *> Lines) { |
| 332 | Block->setCloseName(CloseName, CloseNameLocBegin); |
| 333 | Block->setLines(Lines); |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | VerbatimLineComment *Sema::actOnVerbatimLine(SourceLocation LocBegin, |
Dmitri Gribenko | e4330a3 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 337 | unsigned CommandID, |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 338 | SourceLocation TextBegin, |
| 339 | StringRef Text) { |
| 340 | return new (Allocator) VerbatimLineComment( |
| 341 | LocBegin, |
| 342 | TextBegin.getLocWithOffset(Text.size()), |
Dmitri Gribenko | e4330a3 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 343 | CommandID, |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 344 | TextBegin, |
| 345 | Text); |
| 346 | } |
| 347 | |
Dmitri Gribenko | 3f38bf2 | 2012-07-13 00:44:24 +0000 | [diff] [blame] | 348 | HTMLStartTagComment *Sema::actOnHTMLStartTagStart(SourceLocation LocBegin, |
| 349 | StringRef TagName) { |
| 350 | return new (Allocator) HTMLStartTagComment(LocBegin, TagName); |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 351 | } |
| 352 | |
Dmitri Gribenko | 7d9b511 | 2012-08-06 19:03:12 +0000 | [diff] [blame] | 353 | void Sema::actOnHTMLStartTagFinish( |
Dmitri Gribenko | 3f38bf2 | 2012-07-13 00:44:24 +0000 | [diff] [blame] | 354 | HTMLStartTagComment *Tag, |
| 355 | ArrayRef<HTMLStartTagComment::Attribute> Attrs, |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 356 | SourceLocation GreaterLoc, |
| 357 | bool IsSelfClosing) { |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 358 | Tag->setAttrs(Attrs); |
| 359 | Tag->setGreaterLoc(GreaterLoc); |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 360 | if (IsSelfClosing) |
| 361 | Tag->setSelfClosing(); |
Dmitri Gribenko | 3f38bf2 | 2012-07-13 00:44:24 +0000 | [diff] [blame] | 362 | else if (!isHTMLEndTagForbidden(Tag->getTagName())) |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 363 | HTMLOpenTags.push_back(Tag); |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 364 | } |
| 365 | |
Dmitri Gribenko | 3f38bf2 | 2012-07-13 00:44:24 +0000 | [diff] [blame] | 366 | HTMLEndTagComment *Sema::actOnHTMLEndTag(SourceLocation LocBegin, |
| 367 | SourceLocation LocEnd, |
| 368 | StringRef TagName) { |
| 369 | HTMLEndTagComment *HET = |
| 370 | new (Allocator) HTMLEndTagComment(LocBegin, LocEnd, TagName); |
| 371 | if (isHTMLEndTagForbidden(TagName)) { |
| 372 | Diag(HET->getLocation(), diag::warn_doc_html_end_forbidden) |
| 373 | << TagName << HET->getSourceRange(); |
| 374 | return HET; |
Dmitri Gribenko | 3d98698 | 2012-07-12 23:37:09 +0000 | [diff] [blame] | 375 | } |
| 376 | |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 377 | bool FoundOpen = false; |
Dmitri Gribenko | 3f38bf2 | 2012-07-13 00:44:24 +0000 | [diff] [blame] | 378 | for (SmallVectorImpl<HTMLStartTagComment *>::const_reverse_iterator |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 379 | I = HTMLOpenTags.rbegin(), E = HTMLOpenTags.rend(); |
| 380 | I != E; ++I) { |
| 381 | if ((*I)->getTagName() == TagName) { |
| 382 | FoundOpen = true; |
| 383 | break; |
| 384 | } |
| 385 | } |
| 386 | if (!FoundOpen) { |
Dmitri Gribenko | 3f38bf2 | 2012-07-13 00:44:24 +0000 | [diff] [blame] | 387 | Diag(HET->getLocation(), diag::warn_doc_html_end_unbalanced) |
| 388 | << HET->getSourceRange(); |
| 389 | return HET; |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | while (!HTMLOpenTags.empty()) { |
Dmitri Gribenko | 3f38bf2 | 2012-07-13 00:44:24 +0000 | [diff] [blame] | 393 | const HTMLStartTagComment *HST = HTMLOpenTags.back(); |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 394 | HTMLOpenTags.pop_back(); |
Dmitri Gribenko | 3f38bf2 | 2012-07-13 00:44:24 +0000 | [diff] [blame] | 395 | StringRef LastNotClosedTagName = HST->getTagName(); |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 396 | if (LastNotClosedTagName == TagName) |
| 397 | break; |
| 398 | |
Dmitri Gribenko | 3f38bf2 | 2012-07-13 00:44:24 +0000 | [diff] [blame] | 399 | if (isHTMLEndTagOptional(LastNotClosedTagName)) |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 400 | continue; |
| 401 | |
| 402 | bool OpenLineInvalid; |
| 403 | const unsigned OpenLine = SourceMgr.getPresumedLineNumber( |
Dmitri Gribenko | 3f38bf2 | 2012-07-13 00:44:24 +0000 | [diff] [blame] | 404 | HST->getLocation(), |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 405 | &OpenLineInvalid); |
| 406 | bool CloseLineInvalid; |
| 407 | const unsigned CloseLine = SourceMgr.getPresumedLineNumber( |
Dmitri Gribenko | 3f38bf2 | 2012-07-13 00:44:24 +0000 | [diff] [blame] | 408 | HET->getLocation(), |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 409 | &CloseLineInvalid); |
| 410 | |
| 411 | if (OpenLineInvalid || CloseLineInvalid || OpenLine == CloseLine) |
Dmitri Gribenko | 3f38bf2 | 2012-07-13 00:44:24 +0000 | [diff] [blame] | 412 | Diag(HST->getLocation(), diag::warn_doc_html_start_end_mismatch) |
| 413 | << HST->getTagName() << HET->getTagName() |
| 414 | << HST->getSourceRange() << HET->getSourceRange(); |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 415 | else { |
Dmitri Gribenko | 3f38bf2 | 2012-07-13 00:44:24 +0000 | [diff] [blame] | 416 | Diag(HST->getLocation(), diag::warn_doc_html_start_end_mismatch) |
| 417 | << HST->getTagName() << HET->getTagName() |
| 418 | << HST->getSourceRange(); |
| 419 | Diag(HET->getLocation(), diag::note_doc_html_end_tag) |
| 420 | << HET->getSourceRange(); |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 421 | } |
| 422 | } |
| 423 | |
Dmitri Gribenko | 3f38bf2 | 2012-07-13 00:44:24 +0000 | [diff] [blame] | 424 | return HET; |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | FullComment *Sema::actOnFullComment( |
| 428 | ArrayRef<BlockContentComment *> Blocks) { |
Fariborz Jahanian | 749ace6 | 2012-10-11 23:52:50 +0000 | [diff] [blame] | 429 | FullComment *FC = new (Allocator) FullComment(Blocks, ThisDeclInfo); |
Dmitri Gribenko | 9edd2c8 | 2012-08-24 17:45:39 +0000 | [diff] [blame] | 430 | resolveParamCommandIndexes(FC); |
| 431 | return FC; |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 432 | } |
| 433 | |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 434 | void Sema::checkBlockCommandEmptyParagraph(BlockCommandComment *Command) { |
Dmitri Gribenko | abcf0dc | 2012-09-13 20:36:01 +0000 | [diff] [blame] | 435 | if (Traits.getCommandInfo(Command->getCommandID())->IsEmptyParagraphAllowed) |
| 436 | return; |
| 437 | |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 438 | ParagraphComment *Paragraph = Command->getParagraph(); |
| 439 | if (Paragraph->isWhitespace()) { |
| 440 | SourceLocation DiagLoc; |
Dmitri Gribenko | 0eaf69d | 2012-07-13 19:02:42 +0000 | [diff] [blame] | 441 | if (Command->getNumArgs() > 0) |
| 442 | DiagLoc = Command->getArgRange(Command->getNumArgs() - 1).getEnd(); |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 443 | if (!DiagLoc.isValid()) |
Dmitri Gribenko | e4330a3 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 444 | DiagLoc = Command->getCommandNameRange(Traits).getEnd(); |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 445 | Diag(DiagLoc, diag::warn_doc_block_command_empty_paragraph) |
Dmitri Gribenko | 808383d | 2013-03-04 23:06:15 +0000 | [diff] [blame] | 446 | << Command->getCommandMarker() |
Dmitri Gribenko | e4330a3 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 447 | << Command->getCommandName(Traits) |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 448 | << Command->getSourceRange(); |
| 449 | } |
| 450 | } |
| 451 | |
Dmitri Gribenko | 89ab7d0 | 2012-08-03 21:15:32 +0000 | [diff] [blame] | 452 | void Sema::checkReturnsCommand(const BlockCommandComment *Command) { |
Dmitri Gribenko | e4330a3 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 453 | if (!Traits.getCommandInfo(Command->getCommandID())->IsReturnsCommand) |
Dmitri Gribenko | 89ab7d0 | 2012-08-03 21:15:32 +0000 | [diff] [blame] | 454 | return; |
| 455 | if (isFunctionDecl()) { |
| 456 | if (ThisDeclInfo->ResultType->isVoidType()) { |
| 457 | unsigned DiagKind; |
Fariborz Jahanian | bf967be | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 458 | switch (ThisDeclInfo->CommentDecl->getKind()) { |
Dmitri Gribenko | 89ab7d0 | 2012-08-03 21:15:32 +0000 | [diff] [blame] | 459 | default: |
Dmitri Gribenko | 88815f3 | 2012-08-06 16:29:26 +0000 | [diff] [blame] | 460 | if (ThisDeclInfo->IsObjCMethod) |
| 461 | DiagKind = 3; |
| 462 | else |
| 463 | DiagKind = 0; |
Dmitri Gribenko | 89ab7d0 | 2012-08-03 21:15:32 +0000 | [diff] [blame] | 464 | break; |
| 465 | case Decl::CXXConstructor: |
| 466 | DiagKind = 1; |
| 467 | break; |
| 468 | case Decl::CXXDestructor: |
| 469 | DiagKind = 2; |
| 470 | break; |
| 471 | } |
| 472 | Diag(Command->getLocation(), |
| 473 | diag::warn_doc_returns_attached_to_a_void_function) |
Dmitri Gribenko | 808383d | 2013-03-04 23:06:15 +0000 | [diff] [blame] | 474 | << Command->getCommandMarker() |
Dmitri Gribenko | e4330a3 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 475 | << Command->getCommandName(Traits) |
Dmitri Gribenko | 89ab7d0 | 2012-08-03 21:15:32 +0000 | [diff] [blame] | 476 | << DiagKind |
| 477 | << Command->getSourceRange(); |
| 478 | } |
| 479 | return; |
| 480 | } |
Fariborz Jahanian | 664e860 | 2013-02-27 00:46:06 +0000 | [diff] [blame] | 481 | else if (isObjCPropertyDecl()) |
| 482 | return; |
| 483 | |
Dmitri Gribenko | 89ab7d0 | 2012-08-03 21:15:32 +0000 | [diff] [blame] | 484 | Diag(Command->getLocation(), |
| 485 | diag::warn_doc_returns_not_attached_to_a_function_decl) |
Dmitri Gribenko | 808383d | 2013-03-04 23:06:15 +0000 | [diff] [blame] | 486 | << Command->getCommandMarker() |
Dmitri Gribenko | e4330a3 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 487 | << Command->getCommandName(Traits) |
Dmitri Gribenko | 89ab7d0 | 2012-08-03 21:15:32 +0000 | [diff] [blame] | 488 | << Command->getSourceRange(); |
| 489 | } |
| 490 | |
Dmitri Gribenko | 9443c57 | 2012-08-06 17:08:27 +0000 | [diff] [blame] | 491 | void Sema::checkBlockCommandDuplicate(const BlockCommandComment *Command) { |
Dmitri Gribenko | e4330a3 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 492 | const CommandInfo *Info = Traits.getCommandInfo(Command->getCommandID()); |
Dmitri Gribenko | 9443c57 | 2012-08-06 17:08:27 +0000 | [diff] [blame] | 493 | const BlockCommandComment *PrevCommand = NULL; |
Dmitri Gribenko | e4330a3 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 494 | if (Info->IsBriefCommand) { |
Dmitri Gribenko | 9443c57 | 2012-08-06 17:08:27 +0000 | [diff] [blame] | 495 | if (!BriefCommand) { |
| 496 | BriefCommand = Command; |
| 497 | return; |
| 498 | } |
| 499 | PrevCommand = BriefCommand; |
Dmitri Gribenko | e4330a3 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 500 | } else if (Info->IsReturnsCommand) { |
Dmitri Gribenko | 9443c57 | 2012-08-06 17:08:27 +0000 | [diff] [blame] | 501 | if (!ReturnsCommand) { |
| 502 | ReturnsCommand = Command; |
| 503 | return; |
| 504 | } |
| 505 | PrevCommand = ReturnsCommand; |
Fariborz Jahanian | f843a58 | 2013-01-31 23:12:39 +0000 | [diff] [blame] | 506 | } else if (Info->IsHeaderfileCommand) { |
| 507 | if (!HeaderfileCommand) { |
| 508 | HeaderfileCommand = Command; |
| 509 | return; |
| 510 | } |
| 511 | PrevCommand = HeaderfileCommand; |
Dmitri Gribenko | 9443c57 | 2012-08-06 17:08:27 +0000 | [diff] [blame] | 512 | } else { |
| 513 | // We don't want to check this command for duplicates. |
| 514 | return; |
| 515 | } |
Dmitri Gribenko | e4330a3 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 516 | StringRef CommandName = Command->getCommandName(Traits); |
| 517 | StringRef PrevCommandName = PrevCommand->getCommandName(Traits); |
Dmitri Gribenko | 9443c57 | 2012-08-06 17:08:27 +0000 | [diff] [blame] | 518 | Diag(Command->getLocation(), diag::warn_doc_block_command_duplicate) |
Dmitri Gribenko | 808383d | 2013-03-04 23:06:15 +0000 | [diff] [blame] | 519 | << Command->getCommandMarker() |
Dmitri Gribenko | e4330a3 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 520 | << CommandName |
Dmitri Gribenko | 9443c57 | 2012-08-06 17:08:27 +0000 | [diff] [blame] | 521 | << Command->getSourceRange(); |
Dmitri Gribenko | e4330a3 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 522 | if (CommandName == PrevCommandName) |
Dmitri Gribenko | 9443c57 | 2012-08-06 17:08:27 +0000 | [diff] [blame] | 523 | Diag(PrevCommand->getLocation(), diag::note_doc_block_command_previous) |
Dmitri Gribenko | 808383d | 2013-03-04 23:06:15 +0000 | [diff] [blame] | 524 | << PrevCommand->getCommandMarker() |
Dmitri Gribenko | e4330a3 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 525 | << PrevCommandName |
| 526 | << PrevCommand->getSourceRange(); |
Dmitri Gribenko | 9443c57 | 2012-08-06 17:08:27 +0000 | [diff] [blame] | 527 | else |
| 528 | Diag(PrevCommand->getLocation(), |
| 529 | diag::note_doc_block_command_previous_alias) |
Dmitri Gribenko | 808383d | 2013-03-04 23:06:15 +0000 | [diff] [blame] | 530 | << PrevCommand->getCommandMarker() |
Dmitri Gribenko | e4330a3 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 531 | << PrevCommandName |
| 532 | << CommandName; |
Dmitri Gribenko | 9443c57 | 2012-08-06 17:08:27 +0000 | [diff] [blame] | 533 | } |
| 534 | |
Dmitri Gribenko | 0bd9838 | 2012-09-22 21:47:50 +0000 | [diff] [blame] | 535 | void Sema::checkDeprecatedCommand(const BlockCommandComment *Command) { |
| 536 | if (!Traits.getCommandInfo(Command->getCommandID())->IsDeprecatedCommand) |
| 537 | return; |
| 538 | |
Fariborz Jahanian | bf967be | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 539 | const Decl *D = ThisDeclInfo->CommentDecl; |
Dmitri Gribenko | 0bd9838 | 2012-09-22 21:47:50 +0000 | [diff] [blame] | 540 | if (!D) |
| 541 | return; |
| 542 | |
| 543 | if (D->hasAttr<DeprecatedAttr>() || |
| 544 | D->hasAttr<AvailabilityAttr>() || |
| 545 | D->hasAttr<UnavailableAttr>()) |
| 546 | return; |
| 547 | |
| 548 | Diag(Command->getLocation(), |
| 549 | diag::warn_doc_deprecated_not_sync) |
| 550 | << Command->getSourceRange(); |
| 551 | |
| 552 | // Try to emit a fixit with a deprecation attribute. |
| 553 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 554 | // Don't emit a Fix-It for non-member function definitions. GCC does not |
| 555 | // accept attributes on them. |
| 556 | const DeclContext *Ctx = FD->getDeclContext(); |
| 557 | if ((!Ctx || !Ctx->isRecord()) && |
| 558 | FD->doesThisDeclarationHaveABody()) |
| 559 | return; |
| 560 | |
Dmitri Gribenko | 1952354 | 2012-09-29 11:40:46 +0000 | [diff] [blame] | 561 | StringRef AttributeSpelling = "__attribute__((deprecated))"; |
| 562 | if (PP) { |
| 563 | TokenValue Tokens[] = { |
| 564 | tok::kw___attribute, tok::l_paren, tok::l_paren, |
| 565 | PP->getIdentifierInfo("deprecated"), |
| 566 | tok::r_paren, tok::r_paren |
| 567 | }; |
| 568 | StringRef MacroName = PP->getLastMacroWithSpelling(FD->getLocation(), |
| 569 | Tokens); |
| 570 | if (!MacroName.empty()) |
| 571 | AttributeSpelling = MacroName; |
| 572 | } |
| 573 | |
| 574 | SmallString<64> TextToInsert(" "); |
| 575 | TextToInsert += AttributeSpelling; |
Dmitri Gribenko | 0bd9838 | 2012-09-22 21:47:50 +0000 | [diff] [blame] | 576 | Diag(FD->getLocEnd(), |
| 577 | diag::note_add_deprecation_attr) |
| 578 | << FixItHint::CreateInsertion(FD->getLocEnd().getLocWithOffset(1), |
Dmitri Gribenko | 1952354 | 2012-09-29 11:40:46 +0000 | [diff] [blame] | 579 | TextToInsert); |
Dmitri Gribenko | 0bd9838 | 2012-09-22 21:47:50 +0000 | [diff] [blame] | 580 | } |
| 581 | } |
| 582 | |
Dmitri Gribenko | 9edd2c8 | 2012-08-24 17:45:39 +0000 | [diff] [blame] | 583 | void Sema::resolveParamCommandIndexes(const FullComment *FC) { |
| 584 | if (!isFunctionDecl()) { |
| 585 | // We already warned that \\param commands are not attached to a function |
| 586 | // decl. |
| 587 | return; |
| 588 | } |
| 589 | |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 590 | SmallVector<ParamCommandComment *, 8> UnresolvedParamCommands; |
Dmitri Gribenko | 9edd2c8 | 2012-08-24 17:45:39 +0000 | [diff] [blame] | 591 | |
| 592 | // Comment AST nodes that correspond to \c ParamVars for which we have |
| 593 | // found a \\param command or NULL if no documentation was found so far. |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 594 | SmallVector<ParamCommandComment *, 8> ParamVarDocs; |
Dmitri Gribenko | 9edd2c8 | 2012-08-24 17:45:39 +0000 | [diff] [blame] | 595 | |
| 596 | ArrayRef<const ParmVarDecl *> ParamVars = getParamVars(); |
| 597 | ParamVarDocs.resize(ParamVars.size(), NULL); |
| 598 | |
| 599 | // First pass over all \\param commands: resolve all parameter names. |
| 600 | for (Comment::child_iterator I = FC->child_begin(), E = FC->child_end(); |
| 601 | I != E; ++I) { |
| 602 | ParamCommandComment *PCC = dyn_cast<ParamCommandComment>(*I); |
| 603 | if (!PCC || !PCC->hasParamName()) |
| 604 | continue; |
Fariborz Jahanian | 262e60c | 2012-10-18 21:42:42 +0000 | [diff] [blame] | 605 | StringRef ParamName = PCC->getParamNameAsWritten(); |
Dmitri Gribenko | 9edd2c8 | 2012-08-24 17:45:39 +0000 | [diff] [blame] | 606 | |
| 607 | // Check that referenced parameter name is in the function decl. |
| 608 | const unsigned ResolvedParamIndex = resolveParmVarReference(ParamName, |
| 609 | ParamVars); |
| 610 | if (ResolvedParamIndex == ParamCommandComment::InvalidParamIndex) { |
| 611 | UnresolvedParamCommands.push_back(PCC); |
| 612 | continue; |
| 613 | } |
| 614 | PCC->setParamIndex(ResolvedParamIndex); |
| 615 | if (ParamVarDocs[ResolvedParamIndex]) { |
| 616 | SourceRange ArgRange = PCC->getParamNameRange(); |
| 617 | Diag(ArgRange.getBegin(), diag::warn_doc_param_duplicate) |
| 618 | << ParamName << ArgRange; |
| 619 | ParamCommandComment *PrevCommand = ParamVarDocs[ResolvedParamIndex]; |
| 620 | Diag(PrevCommand->getLocation(), diag::note_doc_param_previous) |
| 621 | << PrevCommand->getParamNameRange(); |
| 622 | } |
| 623 | ParamVarDocs[ResolvedParamIndex] = PCC; |
| 624 | } |
| 625 | |
| 626 | // Find parameter declarations that have no corresponding \\param. |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 627 | SmallVector<const ParmVarDecl *, 8> OrphanedParamDecls; |
Dmitri Gribenko | 9edd2c8 | 2012-08-24 17:45:39 +0000 | [diff] [blame] | 628 | for (unsigned i = 0, e = ParamVarDocs.size(); i != e; ++i) { |
| 629 | if (!ParamVarDocs[i]) |
| 630 | OrphanedParamDecls.push_back(ParamVars[i]); |
| 631 | } |
| 632 | |
| 633 | // Second pass over unresolved \\param commands: do typo correction. |
| 634 | // Suggest corrections from a set of parameter declarations that have no |
| 635 | // corresponding \\param. |
| 636 | for (unsigned i = 0, e = UnresolvedParamCommands.size(); i != e; ++i) { |
| 637 | const ParamCommandComment *PCC = UnresolvedParamCommands[i]; |
| 638 | |
| 639 | SourceRange ArgRange = PCC->getParamNameRange(); |
Fariborz Jahanian | 262e60c | 2012-10-18 21:42:42 +0000 | [diff] [blame] | 640 | StringRef ParamName = PCC->getParamNameAsWritten(); |
Dmitri Gribenko | 9edd2c8 | 2012-08-24 17:45:39 +0000 | [diff] [blame] | 641 | Diag(ArgRange.getBegin(), diag::warn_doc_param_not_found) |
| 642 | << ParamName << ArgRange; |
| 643 | |
| 644 | // All parameters documented -- can't suggest a correction. |
| 645 | if (OrphanedParamDecls.size() == 0) |
| 646 | continue; |
| 647 | |
| 648 | unsigned CorrectedParamIndex = ParamCommandComment::InvalidParamIndex; |
| 649 | if (OrphanedParamDecls.size() == 1) { |
| 650 | // If one parameter is not documented then that parameter is the only |
| 651 | // possible suggestion. |
| 652 | CorrectedParamIndex = 0; |
| 653 | } else { |
| 654 | // Do typo correction. |
| 655 | CorrectedParamIndex = correctTypoInParmVarReference(ParamName, |
| 656 | OrphanedParamDecls); |
| 657 | } |
| 658 | if (CorrectedParamIndex != ParamCommandComment::InvalidParamIndex) { |
| 659 | const ParmVarDecl *CorrectedPVD = OrphanedParamDecls[CorrectedParamIndex]; |
| 660 | if (const IdentifierInfo *CorrectedII = CorrectedPVD->getIdentifier()) |
| 661 | Diag(ArgRange.getBegin(), diag::note_doc_param_name_suggestion) |
| 662 | << CorrectedII->getName() |
| 663 | << FixItHint::CreateReplacement(ArgRange, CorrectedII->getName()); |
| 664 | } |
| 665 | } |
| 666 | } |
| 667 | |
Dmitri Gribenko | 8487c52 | 2012-07-23 17:40:30 +0000 | [diff] [blame] | 668 | bool Sema::isFunctionDecl() { |
Dmitri Gribenko | 1ca7ecc | 2012-08-01 23:08:09 +0000 | [diff] [blame] | 669 | if (!ThisDeclInfo) |
| 670 | return false; |
| 671 | if (!ThisDeclInfo->IsFilled) |
Dmitri Gribenko | 00c59f7 | 2012-07-24 20:58:46 +0000 | [diff] [blame] | 672 | inspectThisDecl(); |
Dmitri Gribenko | af19a6a | 2012-08-02 21:45:39 +0000 | [diff] [blame] | 673 | return ThisDeclInfo->getKind() == DeclInfo::FunctionKind; |
Dmitri Gribenko | 8487c52 | 2012-07-23 17:40:30 +0000 | [diff] [blame] | 674 | } |
Fariborz Jahanian | 664e860 | 2013-02-27 00:46:06 +0000 | [diff] [blame] | 675 | |
| 676 | bool Sema::isObjCPropertyDecl() { |
| 677 | if (!ThisDeclInfo) |
| 678 | return false; |
| 679 | if (!ThisDeclInfo->IsFilled) |
| 680 | inspectThisDecl(); |
| 681 | return ThisDeclInfo->CurrentDecl->getKind() == Decl::ObjCProperty; |
| 682 | } |
Dmitri Gribenko | 8487c52 | 2012-07-23 17:40:30 +0000 | [diff] [blame] | 683 | |
Dmitri Gribenko | 04bf29e | 2012-08-06 21:31:15 +0000 | [diff] [blame] | 684 | bool Sema::isTemplateOrSpecialization() { |
Dmitri Gribenko | 1ca7ecc | 2012-08-01 23:08:09 +0000 | [diff] [blame] | 685 | if (!ThisDeclInfo) |
| 686 | return false; |
| 687 | if (!ThisDeclInfo->IsFilled) |
Dmitri Gribenko | 96b0986 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 688 | inspectThisDecl(); |
Dmitri Gribenko | 04bf29e | 2012-08-06 21:31:15 +0000 | [diff] [blame] | 689 | return ThisDeclInfo->getTemplateKind() != DeclInfo::NotTemplate; |
Dmitri Gribenko | 96b0986 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 690 | } |
| 691 | |
Dmitri Gribenko | 8487c52 | 2012-07-23 17:40:30 +0000 | [diff] [blame] | 692 | ArrayRef<const ParmVarDecl *> Sema::getParamVars() { |
Dmitri Gribenko | 1ca7ecc | 2012-08-01 23:08:09 +0000 | [diff] [blame] | 693 | if (!ThisDeclInfo->IsFilled) |
Dmitri Gribenko | 00c59f7 | 2012-07-24 20:58:46 +0000 | [diff] [blame] | 694 | inspectThisDecl(); |
Dmitri Gribenko | 1ca7ecc | 2012-08-01 23:08:09 +0000 | [diff] [blame] | 695 | return ThisDeclInfo->ParamVars; |
Dmitri Gribenko | 8487c52 | 2012-07-23 17:40:30 +0000 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | void Sema::inspectThisDecl() { |
Dmitri Gribenko | 1ca7ecc | 2012-08-01 23:08:09 +0000 | [diff] [blame] | 699 | ThisDeclInfo->fill(); |
Dmitri Gribenko | 8487c52 | 2012-07-23 17:40:30 +0000 | [diff] [blame] | 700 | } |
| 701 | |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 702 | unsigned Sema::resolveParmVarReference(StringRef Name, |
Dmitri Gribenko | 8487c52 | 2012-07-23 17:40:30 +0000 | [diff] [blame] | 703 | ArrayRef<const ParmVarDecl *> ParamVars) { |
| 704 | for (unsigned i = 0, e = ParamVars.size(); i != e; ++i) { |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 705 | const IdentifierInfo *II = ParamVars[i]->getIdentifier(); |
| 706 | if (II && II->getName() == Name) |
| 707 | return i; |
| 708 | } |
| 709 | return ParamCommandComment::InvalidParamIndex; |
| 710 | } |
| 711 | |
Dmitri Gribenko | 96b0986 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 712 | namespace { |
| 713 | class SimpleTypoCorrector { |
| 714 | StringRef Typo; |
| 715 | const unsigned MaxEditDistance; |
| 716 | |
| 717 | const NamedDecl *BestDecl; |
| 718 | unsigned BestEditDistance; |
| 719 | unsigned BestIndex; |
| 720 | unsigned NextIndex; |
| 721 | |
| 722 | public: |
| 723 | SimpleTypoCorrector(StringRef Typo) : |
| 724 | Typo(Typo), MaxEditDistance((Typo.size() + 2) / 3), |
| 725 | BestDecl(NULL), BestEditDistance(MaxEditDistance + 1), |
| 726 | BestIndex(0), NextIndex(0) |
| 727 | { } |
| 728 | |
| 729 | void addDecl(const NamedDecl *ND); |
| 730 | |
| 731 | const NamedDecl *getBestDecl() const { |
| 732 | if (BestEditDistance > MaxEditDistance) |
| 733 | return NULL; |
| 734 | |
| 735 | return BestDecl; |
| 736 | } |
| 737 | |
| 738 | unsigned getBestDeclIndex() const { |
| 739 | assert(getBestDecl()); |
| 740 | return BestIndex; |
| 741 | } |
| 742 | }; |
| 743 | |
| 744 | void SimpleTypoCorrector::addDecl(const NamedDecl *ND) { |
| 745 | unsigned CurrIndex = NextIndex++; |
| 746 | |
| 747 | const IdentifierInfo *II = ND->getIdentifier(); |
| 748 | if (!II) |
| 749 | return; |
| 750 | |
| 751 | StringRef Name = II->getName(); |
| 752 | unsigned MinPossibleEditDistance = abs((int)Name.size() - (int)Typo.size()); |
| 753 | if (MinPossibleEditDistance > 0 && |
| 754 | Typo.size() / MinPossibleEditDistance < 3) |
| 755 | return; |
| 756 | |
| 757 | unsigned EditDistance = Typo.edit_distance(Name, true, MaxEditDistance); |
| 758 | if (EditDistance < BestEditDistance) { |
| 759 | BestEditDistance = EditDistance; |
| 760 | BestDecl = ND; |
| 761 | BestIndex = CurrIndex; |
| 762 | } |
| 763 | } |
| 764 | } // unnamed namespace |
| 765 | |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 766 | unsigned Sema::correctTypoInParmVarReference( |
| 767 | StringRef Typo, |
Dmitri Gribenko | 8487c52 | 2012-07-23 17:40:30 +0000 | [diff] [blame] | 768 | ArrayRef<const ParmVarDecl *> ParamVars) { |
Dmitri Gribenko | 96b0986 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 769 | SimpleTypoCorrector Corrector(Typo); |
| 770 | for (unsigned i = 0, e = ParamVars.size(); i != e; ++i) |
| 771 | Corrector.addDecl(ParamVars[i]); |
| 772 | if (Corrector.getBestDecl()) |
| 773 | return Corrector.getBestDeclIndex(); |
| 774 | else |
Dmitri Gribenko | 1ad23d6 | 2012-09-10 21:20:09 +0000 | [diff] [blame] | 775 | return ParamCommandComment::InvalidParamIndex; |
Dmitri Gribenko | 96b0986 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 776 | } |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 777 | |
Dmitri Gribenko | 96b0986 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 778 | namespace { |
| 779 | bool ResolveTParamReferenceHelper( |
| 780 | StringRef Name, |
| 781 | const TemplateParameterList *TemplateParameters, |
| 782 | SmallVectorImpl<unsigned> *Position) { |
| 783 | for (unsigned i = 0, e = TemplateParameters->size(); i != e; ++i) { |
| 784 | const NamedDecl *Param = TemplateParameters->getParam(i); |
| 785 | const IdentifierInfo *II = Param->getIdentifier(); |
| 786 | if (II && II->getName() == Name) { |
| 787 | Position->push_back(i); |
| 788 | return true; |
| 789 | } |
| 790 | |
| 791 | if (const TemplateTemplateParmDecl *TTP = |
| 792 | dyn_cast<TemplateTemplateParmDecl>(Param)) { |
| 793 | Position->push_back(i); |
| 794 | if (ResolveTParamReferenceHelper(Name, TTP->getTemplateParameters(), |
| 795 | Position)) |
| 796 | return true; |
| 797 | Position->pop_back(); |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 798 | } |
| 799 | } |
Dmitri Gribenko | 96b0986 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 800 | return false; |
| 801 | } |
| 802 | } // unnamed namespace |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 803 | |
Dmitri Gribenko | 96b0986 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 804 | bool Sema::resolveTParamReference( |
| 805 | StringRef Name, |
| 806 | const TemplateParameterList *TemplateParameters, |
| 807 | SmallVectorImpl<unsigned> *Position) { |
| 808 | Position->clear(); |
| 809 | if (!TemplateParameters) |
| 810 | return false; |
| 811 | |
| 812 | return ResolveTParamReferenceHelper(Name, TemplateParameters, Position); |
| 813 | } |
| 814 | |
| 815 | namespace { |
| 816 | void CorrectTypoInTParamReferenceHelper( |
| 817 | const TemplateParameterList *TemplateParameters, |
| 818 | SimpleTypoCorrector &Corrector) { |
| 819 | for (unsigned i = 0, e = TemplateParameters->size(); i != e; ++i) { |
| 820 | const NamedDecl *Param = TemplateParameters->getParam(i); |
| 821 | Corrector.addDecl(Param); |
| 822 | |
| 823 | if (const TemplateTemplateParmDecl *TTP = |
| 824 | dyn_cast<TemplateTemplateParmDecl>(Param)) |
| 825 | CorrectTypoInTParamReferenceHelper(TTP->getTemplateParameters(), |
| 826 | Corrector); |
| 827 | } |
| 828 | } |
| 829 | } // unnamed namespace |
| 830 | |
| 831 | StringRef Sema::correctTypoInTParamReference( |
| 832 | StringRef Typo, |
| 833 | const TemplateParameterList *TemplateParameters) { |
| 834 | SimpleTypoCorrector Corrector(Typo); |
| 835 | CorrectTypoInTParamReferenceHelper(TemplateParameters, Corrector); |
| 836 | if (const NamedDecl *ND = Corrector.getBestDecl()) { |
| 837 | const IdentifierInfo *II = ND->getIdentifier(); |
| 838 | assert(II && "SimpleTypoCorrector should not return this decl"); |
| 839 | return II->getName(); |
| 840 | } |
| 841 | return StringRef(); |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 842 | } |
| 843 | |
Dmitri Gribenko | 2d66a50 | 2012-07-23 16:43:01 +0000 | [diff] [blame] | 844 | InlineCommandComment::RenderKind |
| 845 | Sema::getInlineCommandRenderKind(StringRef Name) const { |
Dmitri Gribenko | e4330a3 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 846 | assert(Traits.getCommandInfo(Name)->IsInlineCommand); |
Dmitri Gribenko | 2d66a50 | 2012-07-23 16:43:01 +0000 | [diff] [blame] | 847 | |
| 848 | return llvm::StringSwitch<InlineCommandComment::RenderKind>(Name) |
| 849 | .Case("b", InlineCommandComment::RenderBold) |
| 850 | .Cases("c", "p", InlineCommandComment::RenderMonospaced) |
| 851 | .Cases("a", "e", "em", InlineCommandComment::RenderEmphasized) |
| 852 | .Default(InlineCommandComment::RenderNormal); |
| 853 | } |
| 854 | |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 855 | } // end namespace comments |
| 856 | } // end namespace clang |
| 857 | |