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