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"
11 #include "clang/AST/CommentDiagnostic.h"
12 #include "clang/AST/Decl.h"
13 #include "clang/AST/DeclObjC.h"
14 #include "clang/Basic/SourceManager.h"
15 #include "llvm/ADT/StringSwitch.h"
16 
17 namespace clang {
18 namespace comments {
19 
20 Sema::Sema(llvm::BumpPtrAllocator &Allocator, const SourceManager &SourceMgr,
21            DiagnosticsEngine &Diags) :
22     Allocator(Allocator), SourceMgr(SourceMgr), Diags(Diags), ThisDecl(NULL),
23     IsThisDeclInspected(false) {
24 }
25 
26 void Sema::setDecl(const Decl *D) {
27   ThisDecl = D;
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);
52   checkBlockCommandEmptyParagraph(Command);
53   return Command;
54 }
55 
56 ParamCommandComment *Sema::actOnParamCommandStart(SourceLocation LocBegin,
57                                                   SourceLocation LocEnd,
58                                                   StringRef Name) {
59   ParamCommandComment *Command =
60       new (Allocator) ParamCommandComment(LocBegin, LocEnd, Name);
61 
62   if (!isFunctionDecl())
63     Diag(Command->getLocation(),
64          diag::warn_doc_param_not_attached_to_a_function_decl)
65       << Command->getCommandNameRange();
66 
67   return Command;
68 }
69 
70 ParamCommandComment *Sema::actOnParamCommandDirectionArg(
71                                                 ParamCommandComment *Command,
72                                                 SourceLocation ArgLocBegin,
73                                                 SourceLocation ArgLocEnd,
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());
96 
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;
107     } else {
108       Direction = ParamCommandComment::In;
109       RemovingWhitespaceHelped = false;
110     }
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;
122   }
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.
133   assert(Command->getNumArgs() == 0);
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 
145   if (!isFunctionDecl()) {
146     // We already warned that this \\param is not attached to a function decl.
147     return Command;
148   }
149 
150   ArrayRef<const ParmVarDecl *> ParamVars = getParamVars();
151 
152   // Check that referenced parameter name is in the function decl.
153   const unsigned ResolvedParamIndex = resolveParmVarReference(Arg, ParamVars);
154   if (ResolvedParamIndex != ParamCommandComment::InvalidParamIndex) {
155     Command->setParamIndex(ResolvedParamIndex);
156     if (ParamVarDocs[ResolvedParamIndex]) {
157       SourceRange ArgRange(ArgLocBegin, ArgLocEnd);
158       Diag(ArgLocBegin, diag::warn_doc_param_duplicate)
159         << Arg << ArgRange;
160       ParamCommandComment *PrevCommand = ParamVarDocs[ResolvedParamIndex];
161       Diag(PrevCommand->getLocation(), diag::note_doc_param_previous)
162         << PrevCommand->getParamNameRange();
163     }
164     ParamVarDocs[ResolvedParamIndex] = Command;
165     return Command;
166   }
167 
168   SourceRange ArgRange(ArgLocBegin, ArgLocEnd);
169   Diag(ArgLocBegin, diag::warn_doc_param_not_found)
170     << Arg << ArgRange;
171 
172   // No parameters -- can't suggest a correction.
173   if (ParamVars.size() == 0)
174     return Command;
175 
176   unsigned CorrectedParamIndex = ParamCommandComment::InvalidParamIndex;
177   if (ParamVars.size() == 1) {
178     // If function has only one parameter then only that parameter
179     // can be documented.
180     CorrectedParamIndex = 0;
181   } else {
182     // Do typo correction.
183     CorrectedParamIndex = correctTypoInParmVarReference(Arg, ParamVars);
184   }
185   if (CorrectedParamIndex != ParamCommandComment::InvalidParamIndex) {
186     const ParmVarDecl *CorrectedPVD = ParamVars[CorrectedParamIndex];
187     if (const IdentifierInfo *CorrectedII = CorrectedPVD->getIdentifier())
188       Diag(ArgLocBegin, diag::note_doc_param_name_suggestion)
189         << CorrectedII->getName()
190         << FixItHint::CreateReplacement(ArgRange, CorrectedII->getName());
191   }
192 
193   return Command;
194 }
195 
196 ParamCommandComment *Sema::actOnParamCommandFinish(ParamCommandComment *Command,
197                                                    ParagraphComment *Paragraph) {
198   Command->setParagraph(Paragraph);
199   checkBlockCommandEmptyParagraph(Command);
200   return Command;
201 }
202 
203 InlineCommandComment *Sema::actOnInlineCommand(SourceLocation CommandLocBegin,
204                                                SourceLocation CommandLocEnd,
205                                                StringRef CommandName) {
206   ArrayRef<InlineCommandComment::Argument> Args;
207   return new (Allocator) InlineCommandComment(
208                                   CommandLocBegin,
209                                   CommandLocEnd,
210                                   CommandName,
211                                   getInlineCommandRenderKind(CommandName),
212                                   Args);
213 }
214 
215 InlineCommandComment *Sema::actOnInlineCommand(SourceLocation CommandLocBegin,
216                                                SourceLocation CommandLocEnd,
217                                                StringRef CommandName,
218                                                SourceLocation ArgLocBegin,
219                                                SourceLocation ArgLocEnd,
220                                                StringRef Arg) {
221   typedef InlineCommandComment::Argument Argument;
222   Argument *A = new (Allocator) Argument(SourceRange(ArgLocBegin,
223                                                      ArgLocEnd),
224                                          Arg);
225 
226   return new (Allocator) InlineCommandComment(
227                                   CommandLocBegin,
228                                   CommandLocEnd,
229                                   CommandName,
230                                   getInlineCommandRenderKind(CommandName),
231                                   llvm::makeArrayRef(A, 1));
232 }
233 
234 InlineContentComment *Sema::actOnUnknownCommand(SourceLocation LocBegin,
235                                                 SourceLocation LocEnd,
236                                                 StringRef Name) {
237   ArrayRef<InlineCommandComment::Argument> Args;
238   return new (Allocator) InlineCommandComment(
239                                   LocBegin, LocEnd, Name,
240                                   InlineCommandComment::RenderNormal,
241                                   Args);
242 }
243 
244 TextComment *Sema::actOnText(SourceLocation LocBegin,
245                              SourceLocation LocEnd,
246                              StringRef Text) {
247   return new (Allocator) TextComment(LocBegin, LocEnd, Text);
248 }
249 
250 VerbatimBlockComment *Sema::actOnVerbatimBlockStart(SourceLocation Loc,
251                                                     StringRef Name) {
252   return new (Allocator) VerbatimBlockComment(
253                                   Loc,
254                                   Loc.getLocWithOffset(1 + Name.size()),
255                                   Name);
256 }
257 
258 VerbatimBlockLineComment *Sema::actOnVerbatimBlockLine(SourceLocation Loc,
259                                                        StringRef Text) {
260   return new (Allocator) VerbatimBlockLineComment(Loc, Text);
261 }
262 
263 VerbatimBlockComment *Sema::actOnVerbatimBlockFinish(
264                             VerbatimBlockComment *Block,
265                             SourceLocation CloseNameLocBegin,
266                             StringRef CloseName,
267                             ArrayRef<VerbatimBlockLineComment *> Lines) {
268   Block->setCloseName(CloseName, CloseNameLocBegin);
269   Block->setLines(Lines);
270   return Block;
271 }
272 
273 VerbatimLineComment *Sema::actOnVerbatimLine(SourceLocation LocBegin,
274                                              StringRef Name,
275                                              SourceLocation TextBegin,
276                                              StringRef Text) {
277   return new (Allocator) VerbatimLineComment(
278                               LocBegin,
279                               TextBegin.getLocWithOffset(Text.size()),
280                               Name,
281                               TextBegin,
282                               Text);
283 }
284 
285 HTMLStartTagComment *Sema::actOnHTMLStartTagStart(SourceLocation LocBegin,
286                                                   StringRef TagName) {
287   return new (Allocator) HTMLStartTagComment(LocBegin, TagName);
288 }
289 
290 HTMLStartTagComment *Sema::actOnHTMLStartTagFinish(
291                               HTMLStartTagComment *Tag,
292                               ArrayRef<HTMLStartTagComment::Attribute> Attrs,
293                               SourceLocation GreaterLoc,
294                               bool IsSelfClosing) {
295   Tag->setAttrs(Attrs);
296   Tag->setGreaterLoc(GreaterLoc);
297   if (IsSelfClosing)
298     Tag->setSelfClosing();
299   else if (!isHTMLEndTagForbidden(Tag->getTagName()))
300     HTMLOpenTags.push_back(Tag);
301   return Tag;
302 }
303 
304 HTMLEndTagComment *Sema::actOnHTMLEndTag(SourceLocation LocBegin,
305                                          SourceLocation LocEnd,
306                                          StringRef TagName) {
307   HTMLEndTagComment *HET =
308       new (Allocator) HTMLEndTagComment(LocBegin, LocEnd, TagName);
309   if (isHTMLEndTagForbidden(TagName)) {
310     Diag(HET->getLocation(), diag::warn_doc_html_end_forbidden)
311       << TagName << HET->getSourceRange();
312     return HET;
313   }
314 
315   bool FoundOpen = false;
316   for (SmallVectorImpl<HTMLStartTagComment *>::const_reverse_iterator
317        I = HTMLOpenTags.rbegin(), E = HTMLOpenTags.rend();
318        I != E; ++I) {
319     if ((*I)->getTagName() == TagName) {
320       FoundOpen = true;
321       break;
322     }
323   }
324   if (!FoundOpen) {
325     Diag(HET->getLocation(), diag::warn_doc_html_end_unbalanced)
326       << HET->getSourceRange();
327     return HET;
328   }
329 
330   while (!HTMLOpenTags.empty()) {
331     const HTMLStartTagComment *HST = HTMLOpenTags.back();
332     HTMLOpenTags.pop_back();
333     StringRef LastNotClosedTagName = HST->getTagName();
334     if (LastNotClosedTagName == TagName)
335       break;
336 
337     if (isHTMLEndTagOptional(LastNotClosedTagName))
338       continue;
339 
340     bool OpenLineInvalid;
341     const unsigned OpenLine = SourceMgr.getPresumedLineNumber(
342                                                 HST->getLocation(),
343                                                 &OpenLineInvalid);
344     bool CloseLineInvalid;
345     const unsigned CloseLine = SourceMgr.getPresumedLineNumber(
346                                                 HET->getLocation(),
347                                                 &CloseLineInvalid);
348 
349     if (OpenLineInvalid || CloseLineInvalid || OpenLine == CloseLine)
350       Diag(HST->getLocation(), diag::warn_doc_html_start_end_mismatch)
351         << HST->getTagName() << HET->getTagName()
352         << HST->getSourceRange() << HET->getSourceRange();
353     else {
354       Diag(HST->getLocation(), diag::warn_doc_html_start_end_mismatch)
355         << HST->getTagName() << HET->getTagName()
356         << HST->getSourceRange();
357       Diag(HET->getLocation(), diag::note_doc_html_end_tag)
358         << HET->getSourceRange();
359     }
360   }
361 
362   return HET;
363 }
364 
365 FullComment *Sema::actOnFullComment(
366                               ArrayRef<BlockContentComment *> Blocks) {
367   return new (Allocator) FullComment(Blocks);
368 }
369 
370 void Sema::checkBlockCommandEmptyParagraph(BlockCommandComment *Command) {
371   ParagraphComment *Paragraph = Command->getParagraph();
372   if (Paragraph->isWhitespace()) {
373     SourceLocation DiagLoc;
374     if (Command->getNumArgs() > 0)
375       DiagLoc = Command->getArgRange(Command->getNumArgs() - 1).getEnd();
376     if (!DiagLoc.isValid())
377       DiagLoc = Command->getCommandNameRange().getEnd();
378     Diag(DiagLoc, diag::warn_doc_block_command_empty_paragraph)
379       << Command->getCommandName()
380       << Command->getSourceRange();
381   }
382 }
383 
384 bool Sema::isFunctionDecl() {
385   if (!IsThisDeclInspected)
386     inspectThisDecl();
387   return IsFunctionDecl;
388 }
389 
390 ArrayRef<const ParmVarDecl *> Sema::getParamVars() {
391   if (!IsThisDeclInspected)
392     inspectThisDecl();
393   return ParamVars;
394 }
395 
396 void Sema::inspectThisDecl() {
397   assert(!IsThisDeclInspected);
398   if (!ThisDecl) {
399     IsFunctionDecl = false;
400     ParamVars = ArrayRef<const ParmVarDecl *>();
401   } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ThisDecl)) {
402     IsFunctionDecl = true;
403     ParamVars = ArrayRef<const ParmVarDecl *>(FD->param_begin(),
404                                               FD->getNumParams());
405   } else if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(ThisDecl)) {
406     IsFunctionDecl = true;
407     ParamVars = ArrayRef<const ParmVarDecl *>(MD->param_begin(),
408                                               MD->param_size());
409   } else {
410     IsFunctionDecl = false;
411     ParamVars = ArrayRef<const ParmVarDecl *>();
412   }
413   ParamVarDocs.resize(ParamVars.size(), NULL);
414   IsThisDeclInspected = true;
415 }
416 
417 unsigned Sema::resolveParmVarReference(StringRef Name,
418                                        ArrayRef<const ParmVarDecl *> ParamVars) {
419   for (unsigned i = 0, e = ParamVars.size(); i != e; ++i) {
420     const IdentifierInfo *II = ParamVars[i]->getIdentifier();
421     if (II && II->getName() == Name)
422       return i;
423   }
424   return ParamCommandComment::InvalidParamIndex;
425 }
426 
427 unsigned Sema::correctTypoInParmVarReference(
428                                     StringRef Typo,
429                                     ArrayRef<const ParmVarDecl *> ParamVars) {
430   const unsigned MaxEditDistance = (Typo.size() + 2) / 3;
431   unsigned BestPVDIndex = 0;
432   unsigned BestEditDistance = MaxEditDistance + 1;
433   for (unsigned i = 0, e = ParamVars.size(); i != e; ++i) {
434     const IdentifierInfo *II = ParamVars[i]->getIdentifier();
435     if (II) {
436       StringRef Name = II->getName();
437       unsigned MinPossibleEditDistance =
438         abs((int)Name.size() - (int)Typo.size());
439       if (MinPossibleEditDistance > 0 &&
440           Typo.size() / MinPossibleEditDistance < 3)
441         continue;
442 
443       unsigned EditDistance = Typo.edit_distance(Name, true, MaxEditDistance);
444       if (EditDistance < BestEditDistance) {
445         BestEditDistance = EditDistance;
446         BestPVDIndex = i;
447       }
448     }
449   }
450 
451   if (BestEditDistance <= MaxEditDistance)
452     return BestPVDIndex;
453   else
454     return ParamCommandComment::InvalidParamIndex;
455 }
456 
457 // TODO: tablegen
458 bool Sema::isBlockCommand(StringRef Name) {
459   return llvm::StringSwitch<bool>(Name)
460       .Cases("brief", "short", true)
461       .Case("result", true)
462       .Case("return", true)
463       .Case("returns", true)
464       .Case("author", true)
465       .Case("authors", true)
466       .Case("pre", true)
467       .Case("post", true)
468       .Default(false) || isParamCommand(Name);
469 }
470 
471 bool Sema::isParamCommand(StringRef Name) {
472   return llvm::StringSwitch<bool>(Name)
473       .Case("param", true)
474       .Case("arg", true)
475       .Default(false);
476 }
477 
478 unsigned Sema::getBlockCommandNumArgs(StringRef Name) {
479   return llvm::StringSwitch<unsigned>(Name)
480       .Cases("brief", "short", 0)
481       .Case("pre", 0)
482       .Case("post", 0)
483       .Case("author", 0)
484       .Case("authors", 0)
485       .Default(0);
486 }
487 
488 bool Sema::isInlineCommand(StringRef Name) const {
489   return llvm::StringSwitch<bool>(Name)
490       .Case("b", true)
491       .Cases("c", "p", true)
492       .Cases("a", "e", "em", true)
493       .Default(false);
494 }
495 
496 InlineCommandComment::RenderKind
497 Sema::getInlineCommandRenderKind(StringRef Name) const {
498   assert(isInlineCommand(Name));
499 
500   return llvm::StringSwitch<InlineCommandComment::RenderKind>(Name)
501       .Case("b", InlineCommandComment::RenderBold)
502       .Cases("c", "p", InlineCommandComment::RenderMonospaced)
503       .Cases("a", "e", "em", InlineCommandComment::RenderEmphasized)
504       .Default(InlineCommandComment::RenderNormal);
505 }
506 
507 bool Sema::isHTMLEndTagOptional(StringRef Name) {
508   return llvm::StringSwitch<bool>(Name)
509       .Case("p", true)
510       .Case("li", true)
511       .Case("dt", true)
512       .Case("dd", true)
513       .Case("tr", true)
514       .Case("th", true)
515       .Case("td", true)
516       .Case("thead", true)
517       .Case("tfoot", true)
518       .Case("tbody", true)
519       .Case("colgroup", true)
520       .Default(false);
521 }
522 
523 bool Sema::isHTMLEndTagForbidden(StringRef Name) {
524   return llvm::StringSwitch<bool>(Name)
525       .Case("br", true)
526       .Case("hr", true)
527       .Case("img", true)
528       .Case("col", true)
529       .Default(false);
530 }
531 
532 } // end namespace comments
533 } // end namespace clang
534 
535