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