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" |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 11 | #include "clang/AST/CommentDiagnostic.h" |
| 12 | #include "clang/AST/Decl.h" |
| 13 | #include "clang/AST/DeclObjC.h" |
| 14 | #include "clang/Basic/SourceManager.h" |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/StringSwitch.h" |
| 16 | |
| 17 | namespace clang { |
| 18 | namespace comments { |
| 19 | |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 20 | Sema::Sema(llvm::BumpPtrAllocator &Allocator, const SourceManager &SourceMgr, |
| 21 | DiagnosticsEngine &Diags) : |
Dmitri Gribenko | 8487c52 | 2012-07-23 17:40:30 +0000 | [diff] [blame] | 22 | Allocator(Allocator), SourceMgr(SourceMgr), Diags(Diags), ThisDecl(NULL), |
| 23 | IsThisDeclInspected(false) { |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | void Sema::setDecl(const Decl *D) { |
| 27 | ThisDecl = D; |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | ParagraphComment *Sema::actOnParagraphComment( |
| 31 | ArrayRef<InlineContentComment *> Content) { |
| 32 | return new (Allocator) ParagraphComment(Content); |
| 33 | } |
| 34 | |
| 35 | BlockCommandComment *Sema::actOnBlockCommandStart(SourceLocation LocBegin, |
| 36 | SourceLocation LocEnd, |
| 37 | StringRef Name) { |
| 38 | return new (Allocator) BlockCommandComment(LocBegin, LocEnd, Name); |
| 39 | } |
| 40 | |
| 41 | BlockCommandComment *Sema::actOnBlockCommandArgs( |
| 42 | BlockCommandComment *Command, |
| 43 | ArrayRef<BlockCommandComment::Argument> Args) { |
| 44 | Command->setArgs(Args); |
| 45 | return Command; |
| 46 | } |
| 47 | |
| 48 | BlockCommandComment *Sema::actOnBlockCommandFinish( |
| 49 | BlockCommandComment *Command, |
| 50 | ParagraphComment *Paragraph) { |
| 51 | Command->setParagraph(Paragraph); |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 52 | checkBlockCommandEmptyParagraph(Command); |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 53 | return Command; |
| 54 | } |
| 55 | |
| 56 | ParamCommandComment *Sema::actOnParamCommandStart(SourceLocation LocBegin, |
| 57 | SourceLocation LocEnd, |
| 58 | StringRef Name) { |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 59 | ParamCommandComment *Command = |
| 60 | new (Allocator) ParamCommandComment(LocBegin, LocEnd, Name); |
| 61 | |
Dmitri Gribenko | 8487c52 | 2012-07-23 17:40:30 +0000 | [diff] [blame] | 62 | if (!isFunctionDecl()) |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 63 | Diag(Command->getLocation(), |
| 64 | diag::warn_doc_param_not_attached_to_a_function_decl) |
| 65 | << Command->getCommandNameRange(); |
| 66 | |
| 67 | return Command; |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 70 | ParamCommandComment *Sema::actOnParamCommandDirectionArg( |
| 71 | ParamCommandComment *Command, |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 72 | SourceLocation ArgLocBegin, |
| 73 | SourceLocation ArgLocEnd, |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 74 | StringRef Arg) { |
| 75 | ParamCommandComment::PassDirection Direction; |
| 76 | std::string ArgLower = Arg.lower(); |
| 77 | // TODO: optimize: lower Name first (need an API in SmallString for that), |
| 78 | // after that StringSwitch. |
| 79 | if (ArgLower == "[in]") |
| 80 | Direction = ParamCommandComment::In; |
| 81 | else if (ArgLower == "[out]") |
| 82 | Direction = ParamCommandComment::Out; |
| 83 | else if (ArgLower == "[in,out]" || ArgLower == "[out,in]") |
| 84 | Direction = ParamCommandComment::InOut; |
| 85 | else { |
| 86 | // Remove spaces. |
| 87 | std::string::iterator O = ArgLower.begin(); |
| 88 | for (std::string::iterator I = ArgLower.begin(), E = ArgLower.end(); |
| 89 | I != E; ++I) { |
| 90 | const char C = *I; |
| 91 | if (C != ' ' && C != '\n' && C != '\r' && |
| 92 | C != '\t' && C != '\v' && C != '\f') |
| 93 | *O++ = C; |
| 94 | } |
| 95 | ArgLower.resize(O - ArgLower.begin()); |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 96 | |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 97 | bool RemovingWhitespaceHelped = false; |
| 98 | if (ArgLower == "[in]") { |
| 99 | Direction = ParamCommandComment::In; |
| 100 | RemovingWhitespaceHelped = true; |
| 101 | } else if (ArgLower == "[out]") { |
| 102 | Direction = ParamCommandComment::Out; |
| 103 | RemovingWhitespaceHelped = true; |
| 104 | } else if (ArgLower == "[in,out]" || ArgLower == "[out,in]") { |
| 105 | Direction = ParamCommandComment::InOut; |
| 106 | RemovingWhitespaceHelped = true; |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 107 | } else { |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 108 | Direction = ParamCommandComment::In; |
| 109 | RemovingWhitespaceHelped = false; |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 110 | } |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 111 | |
| 112 | SourceRange ArgRange(ArgLocBegin, ArgLocEnd); |
| 113 | if (RemovingWhitespaceHelped) |
| 114 | Diag(ArgLocBegin, diag::warn_doc_param_spaces_in_direction) |
| 115 | << ArgRange |
| 116 | << FixItHint::CreateReplacement( |
| 117 | ArgRange, |
| 118 | ParamCommandComment::getDirectionAsString(Direction)); |
| 119 | else |
| 120 | Diag(ArgLocBegin, diag::warn_doc_param_invalid_direction) |
| 121 | << ArgRange; |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 122 | } |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 123 | Command->setDirection(Direction, /* Explicit = */ true); |
| 124 | return Command; |
| 125 | } |
| 126 | |
| 127 | ParamCommandComment *Sema::actOnParamCommandParamNameArg( |
| 128 | ParamCommandComment *Command, |
| 129 | SourceLocation ArgLocBegin, |
| 130 | SourceLocation ArgLocEnd, |
| 131 | StringRef Arg) { |
| 132 | // Parser will not feed us more arguments than needed. |
Dmitri Gribenko | 0eaf69d | 2012-07-13 19:02:42 +0000 | [diff] [blame] | 133 | assert(Command->getNumArgs() == 0); |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 134 | |
| 135 | if (!Command->isDirectionExplicit()) { |
| 136 | // User didn't provide a direction argument. |
| 137 | Command->setDirection(ParamCommandComment::In, /* Explicit = */ false); |
| 138 | } |
| 139 | typedef BlockCommandComment::Argument Argument; |
| 140 | Argument *A = new (Allocator) Argument(SourceRange(ArgLocBegin, |
| 141 | ArgLocEnd), |
| 142 | Arg); |
| 143 | Command->setArgs(llvm::makeArrayRef(A, 1)); |
| 144 | |
Dmitri Gribenko | 8487c52 | 2012-07-23 17:40:30 +0000 | [diff] [blame] | 145 | if (!isFunctionDecl()) { |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 146 | // We already warned that this \\param is not attached to a function decl. |
| 147 | return Command; |
| 148 | } |
| 149 | |
Dmitri Gribenko | 8487c52 | 2012-07-23 17:40:30 +0000 | [diff] [blame] | 150 | ArrayRef<const ParmVarDecl *> ParamVars = getParamVars(); |
| 151 | |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 152 | // Check that referenced parameter name is in the function decl. |
Dmitri Gribenko | 8487c52 | 2012-07-23 17:40:30 +0000 | [diff] [blame] | 153 | const unsigned ResolvedParamIndex = resolveParmVarReference(Arg, ParamVars); |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 154 | if (ResolvedParamIndex != ParamCommandComment::InvalidParamIndex) { |
| 155 | Command->setParamIndex(ResolvedParamIndex); |
| 156 | return Command; |
| 157 | } |
| 158 | |
| 159 | SourceRange ArgRange(ArgLocBegin, ArgLocEnd); |
| 160 | Diag(ArgLocBegin, diag::warn_doc_param_not_found) |
| 161 | << Arg << ArgRange; |
| 162 | |
| 163 | unsigned CorrectedParamIndex = ParamCommandComment::InvalidParamIndex; |
Dmitri Gribenko | 8487c52 | 2012-07-23 17:40:30 +0000 | [diff] [blame] | 164 | if (ParamVars.size() == 1) { |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 165 | // If function has only one parameter then only that parameter |
| 166 | // can be documented. |
| 167 | CorrectedParamIndex = 0; |
| 168 | } else { |
| 169 | // Do typo correction. |
Dmitri Gribenko | 8487c52 | 2012-07-23 17:40:30 +0000 | [diff] [blame] | 170 | CorrectedParamIndex = correctTypoInParmVarReference(Arg, ParamVars); |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 171 | } |
| 172 | if (CorrectedParamIndex != ParamCommandComment::InvalidParamIndex) { |
| 173 | const ParmVarDecl *CorrectedPVD = ParamVars[CorrectedParamIndex]; |
| 174 | if (const IdentifierInfo *CorrectedII = CorrectedPVD->getIdentifier()) |
| 175 | Diag(ArgLocBegin, diag::note_doc_param_name_suggestion) |
| 176 | << CorrectedII->getName() |
| 177 | << FixItHint::CreateReplacement(ArgRange, CorrectedII->getName()); |
| 178 | } |
| 179 | |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 180 | return Command; |
| 181 | } |
| 182 | |
| 183 | ParamCommandComment *Sema::actOnParamCommandFinish(ParamCommandComment *Command, |
| 184 | ParagraphComment *Paragraph) { |
| 185 | Command->setParagraph(Paragraph); |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 186 | checkBlockCommandEmptyParagraph(Command); |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 187 | return Command; |
| 188 | } |
| 189 | |
| 190 | InlineCommandComment *Sema::actOnInlineCommand(SourceLocation CommandLocBegin, |
| 191 | SourceLocation CommandLocEnd, |
| 192 | StringRef CommandName) { |
| 193 | ArrayRef<InlineCommandComment::Argument> Args; |
Dmitri Gribenko | 2d66a50 | 2012-07-23 16:43:01 +0000 | [diff] [blame] | 194 | return new (Allocator) InlineCommandComment( |
| 195 | CommandLocBegin, |
| 196 | CommandLocEnd, |
| 197 | CommandName, |
| 198 | getInlineCommandRenderKind(CommandName), |
| 199 | Args); |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | InlineCommandComment *Sema::actOnInlineCommand(SourceLocation CommandLocBegin, |
| 203 | SourceLocation CommandLocEnd, |
| 204 | StringRef CommandName, |
| 205 | SourceLocation ArgLocBegin, |
| 206 | SourceLocation ArgLocEnd, |
| 207 | StringRef Arg) { |
| 208 | typedef InlineCommandComment::Argument Argument; |
| 209 | Argument *A = new (Allocator) Argument(SourceRange(ArgLocBegin, |
| 210 | ArgLocEnd), |
| 211 | Arg); |
| 212 | |
Dmitri Gribenko | 2d66a50 | 2012-07-23 16:43:01 +0000 | [diff] [blame] | 213 | return new (Allocator) InlineCommandComment( |
| 214 | CommandLocBegin, |
| 215 | CommandLocEnd, |
| 216 | CommandName, |
| 217 | getInlineCommandRenderKind(CommandName), |
| 218 | llvm::makeArrayRef(A, 1)); |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | InlineContentComment *Sema::actOnUnknownCommand(SourceLocation LocBegin, |
| 222 | SourceLocation LocEnd, |
| 223 | StringRef Name) { |
| 224 | ArrayRef<InlineCommandComment::Argument> Args; |
Dmitri Gribenko | 2d66a50 | 2012-07-23 16:43:01 +0000 | [diff] [blame] | 225 | return new (Allocator) InlineCommandComment( |
| 226 | LocBegin, LocEnd, Name, |
| 227 | InlineCommandComment::RenderNormal, |
| 228 | Args); |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | TextComment *Sema::actOnText(SourceLocation LocBegin, |
| 232 | SourceLocation LocEnd, |
| 233 | StringRef Text) { |
| 234 | return new (Allocator) TextComment(LocBegin, LocEnd, Text); |
| 235 | } |
| 236 | |
| 237 | VerbatimBlockComment *Sema::actOnVerbatimBlockStart(SourceLocation Loc, |
| 238 | StringRef Name) { |
| 239 | return new (Allocator) VerbatimBlockComment( |
| 240 | Loc, |
| 241 | Loc.getLocWithOffset(1 + Name.size()), |
| 242 | Name); |
| 243 | } |
| 244 | |
| 245 | VerbatimBlockLineComment *Sema::actOnVerbatimBlockLine(SourceLocation Loc, |
| 246 | StringRef Text) { |
| 247 | return new (Allocator) VerbatimBlockLineComment(Loc, Text); |
| 248 | } |
| 249 | |
| 250 | VerbatimBlockComment *Sema::actOnVerbatimBlockFinish( |
| 251 | VerbatimBlockComment *Block, |
| 252 | SourceLocation CloseNameLocBegin, |
| 253 | StringRef CloseName, |
| 254 | ArrayRef<VerbatimBlockLineComment *> Lines) { |
| 255 | Block->setCloseName(CloseName, CloseNameLocBegin); |
| 256 | Block->setLines(Lines); |
| 257 | return Block; |
| 258 | } |
| 259 | |
| 260 | VerbatimLineComment *Sema::actOnVerbatimLine(SourceLocation LocBegin, |
| 261 | StringRef Name, |
| 262 | SourceLocation TextBegin, |
| 263 | StringRef Text) { |
| 264 | return new (Allocator) VerbatimLineComment( |
| 265 | LocBegin, |
| 266 | TextBegin.getLocWithOffset(Text.size()), |
| 267 | Name, |
| 268 | TextBegin, |
| 269 | Text); |
| 270 | } |
| 271 | |
Dmitri Gribenko | 3f38bf2 | 2012-07-13 00:44:24 +0000 | [diff] [blame] | 272 | HTMLStartTagComment *Sema::actOnHTMLStartTagStart(SourceLocation LocBegin, |
| 273 | StringRef TagName) { |
| 274 | return new (Allocator) HTMLStartTagComment(LocBegin, TagName); |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 275 | } |
| 276 | |
Dmitri Gribenko | 3f38bf2 | 2012-07-13 00:44:24 +0000 | [diff] [blame] | 277 | HTMLStartTagComment *Sema::actOnHTMLStartTagFinish( |
| 278 | HTMLStartTagComment *Tag, |
| 279 | ArrayRef<HTMLStartTagComment::Attribute> Attrs, |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 280 | SourceLocation GreaterLoc, |
| 281 | bool IsSelfClosing) { |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 282 | Tag->setAttrs(Attrs); |
| 283 | Tag->setGreaterLoc(GreaterLoc); |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 284 | if (IsSelfClosing) |
| 285 | Tag->setSelfClosing(); |
Dmitri Gribenko | 3f38bf2 | 2012-07-13 00:44:24 +0000 | [diff] [blame] | 286 | else if (!isHTMLEndTagForbidden(Tag->getTagName())) |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 287 | HTMLOpenTags.push_back(Tag); |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 288 | return Tag; |
| 289 | } |
| 290 | |
Dmitri Gribenko | 3f38bf2 | 2012-07-13 00:44:24 +0000 | [diff] [blame] | 291 | HTMLEndTagComment *Sema::actOnHTMLEndTag(SourceLocation LocBegin, |
| 292 | SourceLocation LocEnd, |
| 293 | StringRef TagName) { |
| 294 | HTMLEndTagComment *HET = |
| 295 | new (Allocator) HTMLEndTagComment(LocBegin, LocEnd, TagName); |
| 296 | if (isHTMLEndTagForbidden(TagName)) { |
| 297 | Diag(HET->getLocation(), diag::warn_doc_html_end_forbidden) |
| 298 | << TagName << HET->getSourceRange(); |
| 299 | return HET; |
Dmitri Gribenko | 3d98698 | 2012-07-12 23:37:09 +0000 | [diff] [blame] | 300 | } |
| 301 | |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 302 | bool FoundOpen = false; |
Dmitri Gribenko | 3f38bf2 | 2012-07-13 00:44:24 +0000 | [diff] [blame] | 303 | for (SmallVectorImpl<HTMLStartTagComment *>::const_reverse_iterator |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 304 | I = HTMLOpenTags.rbegin(), E = HTMLOpenTags.rend(); |
| 305 | I != E; ++I) { |
| 306 | if ((*I)->getTagName() == TagName) { |
| 307 | FoundOpen = true; |
| 308 | break; |
| 309 | } |
| 310 | } |
| 311 | if (!FoundOpen) { |
Dmitri Gribenko | 3f38bf2 | 2012-07-13 00:44:24 +0000 | [diff] [blame] | 312 | Diag(HET->getLocation(), diag::warn_doc_html_end_unbalanced) |
| 313 | << HET->getSourceRange(); |
| 314 | return HET; |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | while (!HTMLOpenTags.empty()) { |
Dmitri Gribenko | 3f38bf2 | 2012-07-13 00:44:24 +0000 | [diff] [blame] | 318 | const HTMLStartTagComment *HST = HTMLOpenTags.back(); |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 319 | HTMLOpenTags.pop_back(); |
Dmitri Gribenko | 3f38bf2 | 2012-07-13 00:44:24 +0000 | [diff] [blame] | 320 | StringRef LastNotClosedTagName = HST->getTagName(); |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 321 | if (LastNotClosedTagName == TagName) |
| 322 | break; |
| 323 | |
Dmitri Gribenko | 3f38bf2 | 2012-07-13 00:44:24 +0000 | [diff] [blame] | 324 | if (isHTMLEndTagOptional(LastNotClosedTagName)) |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 325 | continue; |
| 326 | |
| 327 | bool OpenLineInvalid; |
| 328 | const unsigned OpenLine = SourceMgr.getPresumedLineNumber( |
Dmitri Gribenko | 3f38bf2 | 2012-07-13 00:44:24 +0000 | [diff] [blame] | 329 | HST->getLocation(), |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 330 | &OpenLineInvalid); |
| 331 | bool CloseLineInvalid; |
| 332 | const unsigned CloseLine = SourceMgr.getPresumedLineNumber( |
Dmitri Gribenko | 3f38bf2 | 2012-07-13 00:44:24 +0000 | [diff] [blame] | 333 | HET->getLocation(), |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 334 | &CloseLineInvalid); |
| 335 | |
| 336 | if (OpenLineInvalid || CloseLineInvalid || OpenLine == CloseLine) |
Dmitri Gribenko | 3f38bf2 | 2012-07-13 00:44:24 +0000 | [diff] [blame] | 337 | Diag(HST->getLocation(), diag::warn_doc_html_start_end_mismatch) |
| 338 | << HST->getTagName() << HET->getTagName() |
| 339 | << HST->getSourceRange() << HET->getSourceRange(); |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 340 | else { |
Dmitri Gribenko | 3f38bf2 | 2012-07-13 00:44:24 +0000 | [diff] [blame] | 341 | Diag(HST->getLocation(), diag::warn_doc_html_start_end_mismatch) |
| 342 | << HST->getTagName() << HET->getTagName() |
| 343 | << HST->getSourceRange(); |
| 344 | Diag(HET->getLocation(), diag::note_doc_html_end_tag) |
| 345 | << HET->getSourceRange(); |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 346 | } |
| 347 | } |
| 348 | |
Dmitri Gribenko | 3f38bf2 | 2012-07-13 00:44:24 +0000 | [diff] [blame] | 349 | return HET; |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | FullComment *Sema::actOnFullComment( |
| 353 | ArrayRef<BlockContentComment *> Blocks) { |
Dmitri Gribenko | 8487c52 | 2012-07-23 17:40:30 +0000 | [diff] [blame] | 354 | SmallVector<ParamCommandComment *, 8> Params; |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 355 | return new (Allocator) FullComment(Blocks); |
| 356 | } |
| 357 | |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 358 | void Sema::checkBlockCommandEmptyParagraph(BlockCommandComment *Command) { |
| 359 | ParagraphComment *Paragraph = Command->getParagraph(); |
| 360 | if (Paragraph->isWhitespace()) { |
| 361 | SourceLocation DiagLoc; |
Dmitri Gribenko | 0eaf69d | 2012-07-13 19:02:42 +0000 | [diff] [blame] | 362 | if (Command->getNumArgs() > 0) |
| 363 | DiagLoc = Command->getArgRange(Command->getNumArgs() - 1).getEnd(); |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 364 | if (!DiagLoc.isValid()) |
| 365 | DiagLoc = Command->getCommandNameRange().getEnd(); |
| 366 | Diag(DiagLoc, diag::warn_doc_block_command_empty_paragraph) |
| 367 | << Command->getCommandName() |
| 368 | << Command->getSourceRange(); |
| 369 | } |
| 370 | } |
| 371 | |
Dmitri Gribenko | 8487c52 | 2012-07-23 17:40:30 +0000 | [diff] [blame] | 372 | bool Sema::isFunctionDecl() { |
Dmitri Gribenko | 00c59f7 | 2012-07-24 20:58:46 +0000 | [diff] [blame^] | 373 | if (!IsThisDeclInspected) |
| 374 | inspectThisDecl(); |
Dmitri Gribenko | 8487c52 | 2012-07-23 17:40:30 +0000 | [diff] [blame] | 375 | return IsFunctionDecl; |
| 376 | } |
| 377 | |
| 378 | ArrayRef<const ParmVarDecl *> Sema::getParamVars() { |
Dmitri Gribenko | 00c59f7 | 2012-07-24 20:58:46 +0000 | [diff] [blame^] | 379 | if (!IsThisDeclInspected) |
| 380 | inspectThisDecl(); |
Dmitri Gribenko | 8487c52 | 2012-07-23 17:40:30 +0000 | [diff] [blame] | 381 | return ParamVars; |
| 382 | } |
| 383 | |
| 384 | void Sema::inspectThisDecl() { |
| 385 | if (!ThisDecl) { |
| 386 | IsFunctionDecl = false; |
| 387 | ParamVars = ArrayRef<const ParmVarDecl *>(); |
| 388 | } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ThisDecl)) { |
| 389 | IsFunctionDecl = true; |
| 390 | ParamVars = ArrayRef<const ParmVarDecl *>(FD->param_begin(), |
| 391 | FD->getNumParams()); |
| 392 | } else if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(ThisDecl)) { |
| 393 | IsFunctionDecl = true; |
| 394 | ParamVars = ArrayRef<const ParmVarDecl *>(MD->param_begin(), |
| 395 | MD->param_size()); |
| 396 | } else { |
| 397 | IsFunctionDecl = false; |
| 398 | ParamVars = ArrayRef<const ParmVarDecl *>(); |
| 399 | } |
| 400 | IsThisDeclInspected = true; |
| 401 | } |
| 402 | |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 403 | unsigned Sema::resolveParmVarReference(StringRef Name, |
Dmitri Gribenko | 8487c52 | 2012-07-23 17:40:30 +0000 | [diff] [blame] | 404 | ArrayRef<const ParmVarDecl *> ParamVars) { |
| 405 | for (unsigned i = 0, e = ParamVars.size(); i != e; ++i) { |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 406 | const IdentifierInfo *II = ParamVars[i]->getIdentifier(); |
| 407 | if (II && II->getName() == Name) |
| 408 | return i; |
| 409 | } |
| 410 | return ParamCommandComment::InvalidParamIndex; |
| 411 | } |
| 412 | |
| 413 | unsigned Sema::correctTypoInParmVarReference( |
| 414 | StringRef Typo, |
Dmitri Gribenko | 8487c52 | 2012-07-23 17:40:30 +0000 | [diff] [blame] | 415 | ArrayRef<const ParmVarDecl *> ParamVars) { |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 416 | const unsigned MaxEditDistance = (Typo.size() + 2) / 3; |
Richard Smith | 18b7f95 | 2012-07-11 22:33:59 +0000 | [diff] [blame] | 417 | unsigned BestPVDIndex = 0; |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 418 | unsigned BestEditDistance = MaxEditDistance + 1; |
Dmitri Gribenko | 8487c52 | 2012-07-23 17:40:30 +0000 | [diff] [blame] | 419 | for (unsigned i = 0, e = ParamVars.size(); i != e; ++i) { |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 420 | const IdentifierInfo *II = ParamVars[i]->getIdentifier(); |
| 421 | if (II) { |
| 422 | StringRef Name = II->getName(); |
NAKAMURA Takumi | dc5796c | 2012-07-12 00:45:08 +0000 | [diff] [blame] | 423 | unsigned MinPossibleEditDistance = |
| 424 | abs((int)Name.size() - (int)Typo.size()); |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 425 | if (MinPossibleEditDistance > 0 && |
| 426 | Typo.size() / MinPossibleEditDistance < 3) |
| 427 | continue; |
| 428 | |
| 429 | unsigned EditDistance = Typo.edit_distance(Name, true, MaxEditDistance); |
| 430 | if (EditDistance < BestEditDistance) { |
| 431 | BestEditDistance = EditDistance; |
| 432 | BestPVDIndex = i; |
| 433 | } |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | if (BestEditDistance <= MaxEditDistance) |
| 438 | return BestPVDIndex; |
| 439 | else |
| 440 | return ParamCommandComment::InvalidParamIndex;; |
| 441 | } |
| 442 | |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 443 | // TODO: tablegen |
| 444 | bool Sema::isBlockCommand(StringRef Name) { |
| 445 | return llvm::StringSwitch<bool>(Name) |
Dmitri Gribenko | 3d3d22c | 2012-07-18 00:44:55 +0000 | [diff] [blame] | 446 | .Cases("brief", "short", true) |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 447 | .Case("result", true) |
| 448 | .Case("return", true) |
| 449 | .Case("returns", true) |
| 450 | .Case("author", true) |
| 451 | .Case("authors", true) |
| 452 | .Case("pre", true) |
| 453 | .Case("post", true) |
| 454 | .Default(false) || isParamCommand(Name); |
| 455 | } |
| 456 | |
| 457 | bool Sema::isParamCommand(StringRef Name) { |
| 458 | return llvm::StringSwitch<bool>(Name) |
| 459 | .Case("param", true) |
| 460 | .Case("arg", true) |
| 461 | .Default(false); |
| 462 | } |
| 463 | |
| 464 | unsigned Sema::getBlockCommandNumArgs(StringRef Name) { |
| 465 | return llvm::StringSwitch<unsigned>(Name) |
Dmitri Gribenko | 3d3d22c | 2012-07-18 00:44:55 +0000 | [diff] [blame] | 466 | .Cases("brief", "short", 0) |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 467 | .Case("pre", 0) |
| 468 | .Case("post", 0) |
| 469 | .Case("author", 0) |
| 470 | .Case("authors", 0) |
| 471 | .Default(0); |
| 472 | } |
| 473 | |
Dmitri Gribenko | 2d66a50 | 2012-07-23 16:43:01 +0000 | [diff] [blame] | 474 | bool Sema::isInlineCommand(StringRef Name) const { |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 475 | return llvm::StringSwitch<bool>(Name) |
Dmitri Gribenko | c48dd8e | 2012-07-19 00:21:03 +0000 | [diff] [blame] | 476 | .Case("b", true) |
| 477 | .Cases("c", "p", true) |
| 478 | .Cases("a", "e", "em", true) |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 479 | .Default(false); |
| 480 | } |
| 481 | |
Dmitri Gribenko | 2d66a50 | 2012-07-23 16:43:01 +0000 | [diff] [blame] | 482 | InlineCommandComment::RenderKind |
| 483 | Sema::getInlineCommandRenderKind(StringRef Name) const { |
| 484 | assert(isInlineCommand(Name)); |
| 485 | |
| 486 | return llvm::StringSwitch<InlineCommandComment::RenderKind>(Name) |
| 487 | .Case("b", InlineCommandComment::RenderBold) |
| 488 | .Cases("c", "p", InlineCommandComment::RenderMonospaced) |
| 489 | .Cases("a", "e", "em", InlineCommandComment::RenderEmphasized) |
| 490 | .Default(InlineCommandComment::RenderNormal); |
| 491 | } |
| 492 | |
Dmitri Gribenko | 3f38bf2 | 2012-07-13 00:44:24 +0000 | [diff] [blame] | 493 | bool Sema::isHTMLEndTagOptional(StringRef Name) { |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 494 | return llvm::StringSwitch<bool>(Name) |
Dmitri Gribenko | 3d98698 | 2012-07-12 23:37:09 +0000 | [diff] [blame] | 495 | .Case("p", true) |
| 496 | .Case("li", true) |
| 497 | .Case("dt", true) |
| 498 | .Case("dd", true) |
| 499 | .Case("tr", true) |
| 500 | .Case("th", true) |
| 501 | .Case("td", true) |
| 502 | .Case("thead", true) |
| 503 | .Case("tfoot", true) |
| 504 | .Case("tbody", true) |
| 505 | .Case("colgroup", true) |
| 506 | .Default(false); |
| 507 | } |
| 508 | |
Dmitri Gribenko | 3f38bf2 | 2012-07-13 00:44:24 +0000 | [diff] [blame] | 509 | bool Sema::isHTMLEndTagForbidden(StringRef Name) { |
Dmitri Gribenko | 3d98698 | 2012-07-12 23:37:09 +0000 | [diff] [blame] | 510 | return llvm::StringSwitch<bool>(Name) |
| 511 | .Case("br", true) |
| 512 | .Case("hr", true) |
| 513 | .Case("img", true) |
| 514 | .Case("col", true) |
| 515 | .Default(false); |
Dmitri Gribenko | 8d3ba23 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | } // end namespace comments |
| 519 | } // end namespace clang |
| 520 | |