2012-06-27 04:39:18 +08:00
|
|
|
//===--- CommentBriefParser.cpp - Dumb comment parser ---------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2012-06-27 04:39:18 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/AST/CommentBriefParser.h"
|
2012-08-09 08:03:17 +08:00
|
|
|
#include "clang/AST/CommentCommandTraits.h"
|
2012-06-27 04:39:18 +08:00
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace comments {
|
|
|
|
|
2012-06-28 09:38:21 +08:00
|
|
|
namespace {
|
2012-08-22 05:15:34 +08:00
|
|
|
inline bool isWhitespace(char C) {
|
|
|
|
return C == ' ' || C == '\n' || C == '\r' ||
|
|
|
|
C == '\t' || C == '\f' || C == '\v';
|
|
|
|
}
|
|
|
|
|
2012-06-28 09:38:21 +08:00
|
|
|
/// Convert all whitespace into spaces, remove leading and trailing spaces,
|
|
|
|
/// compress multiple spaces into one.
|
|
|
|
void cleanupBrief(std::string &S) {
|
|
|
|
bool PrevWasSpace = true;
|
|
|
|
std::string::iterator O = S.begin();
|
|
|
|
for (std::string::iterator I = S.begin(), E = S.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
const char C = *I;
|
2012-08-22 05:15:34 +08:00
|
|
|
if (isWhitespace(C)) {
|
2012-06-28 09:38:21 +08:00
|
|
|
if (!PrevWasSpace) {
|
|
|
|
*O++ = ' ';
|
|
|
|
PrevWasSpace = true;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
*O++ = C;
|
|
|
|
PrevWasSpace = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (O != S.begin() && *(O - 1) == ' ')
|
|
|
|
--O;
|
|
|
|
|
|
|
|
S.resize(O - S.begin());
|
|
|
|
}
|
2012-08-22 05:15:34 +08:00
|
|
|
|
|
|
|
bool isWhitespace(StringRef Text) {
|
|
|
|
for (StringRef::const_iterator I = Text.begin(), E = Text.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
if (!isWhitespace(*I))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2012-08-09 08:03:17 +08:00
|
|
|
} // unnamed namespace
|
2012-06-30 02:19:20 +08:00
|
|
|
|
2012-08-09 08:03:17 +08:00
|
|
|
BriefParser::BriefParser(Lexer &L, const CommandTraits &Traits) :
|
|
|
|
L(L), Traits(Traits) {
|
|
|
|
// Get lookahead token.
|
|
|
|
ConsumeToken();
|
2012-06-30 02:19:20 +08:00
|
|
|
}
|
2012-06-28 09:38:21 +08:00
|
|
|
|
2012-06-27 04:39:18 +08:00
|
|
|
std::string BriefParser::Parse() {
|
2012-07-21 01:01:34 +08:00
|
|
|
std::string FirstParagraphOrBrief;
|
|
|
|
std::string ReturnsParagraph;
|
2012-06-27 04:39:18 +08:00
|
|
|
bool InFirstParagraph = true;
|
|
|
|
bool InBrief = false;
|
2012-07-21 01:01:34 +08:00
|
|
|
bool InReturns = false;
|
2012-06-27 04:39:18 +08:00
|
|
|
|
|
|
|
while (Tok.isNot(tok::eof)) {
|
|
|
|
if (Tok.is(tok::text)) {
|
2012-06-27 09:17:34 +08:00
|
|
|
if (InFirstParagraph || InBrief)
|
2012-07-21 01:01:34 +08:00
|
|
|
FirstParagraphOrBrief += Tok.getText();
|
|
|
|
else if (InReturns)
|
|
|
|
ReturnsParagraph += Tok.getText();
|
2012-06-27 04:39:18 +08:00
|
|
|
ConsumeToken();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-03-02 10:39:57 +08:00
|
|
|
if (Tok.is(tok::backslash_command) || Tok.is(tok::at_command)) {
|
2012-09-11 04:32:42 +08:00
|
|
|
const CommandInfo *Info = Traits.getCommandInfo(Tok.getCommandID());
|
|
|
|
if (Info->IsBriefCommand) {
|
2012-07-21 01:01:34 +08:00
|
|
|
FirstParagraphOrBrief.clear();
|
2012-06-28 08:01:41 +08:00
|
|
|
InBrief = true;
|
|
|
|
ConsumeToken();
|
|
|
|
continue;
|
|
|
|
}
|
2012-09-11 04:32:42 +08:00
|
|
|
if (Info->IsReturnsCommand) {
|
2012-07-21 01:01:34 +08:00
|
|
|
InReturns = true;
|
2012-08-22 05:15:34 +08:00
|
|
|
InBrief = false;
|
|
|
|
InFirstParagraph = false;
|
2012-07-21 01:01:34 +08:00
|
|
|
ReturnsParagraph += "Returns ";
|
2012-08-22 05:15:34 +08:00
|
|
|
ConsumeToken();
|
|
|
|
continue;
|
2012-07-21 01:01:34 +08:00
|
|
|
}
|
2012-06-30 02:19:20 +08:00
|
|
|
// Block commands implicitly start a new paragraph.
|
2012-09-11 04:32:42 +08:00
|
|
|
if (Info->IsBlockCommand) {
|
2012-06-28 08:01:41 +08:00
|
|
|
// We found an implicit paragraph end.
|
|
|
|
InFirstParagraph = false;
|
2012-07-04 02:10:20 +08:00
|
|
|
if (InBrief)
|
2012-06-28 08:01:41 +08:00
|
|
|
break;
|
|
|
|
}
|
2012-06-27 04:39:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Tok.is(tok::newline)) {
|
2012-06-27 09:17:34 +08:00
|
|
|
if (InFirstParagraph || InBrief)
|
2012-07-21 01:01:34 +08:00
|
|
|
FirstParagraphOrBrief += ' ';
|
|
|
|
else if (InReturns)
|
|
|
|
ReturnsParagraph += ' ';
|
2012-06-27 04:39:18 +08:00
|
|
|
ConsumeToken();
|
|
|
|
|
2012-08-22 05:15:34 +08:00
|
|
|
// If the next token is a whitespace only text, ignore it. Thus we allow
|
|
|
|
// two paragraphs to be separated by line that has only whitespace in it.
|
|
|
|
//
|
|
|
|
// We don't need to add a space to the parsed text because we just added
|
|
|
|
// a space for the newline.
|
|
|
|
if (Tok.is(tok::text)) {
|
|
|
|
if (isWhitespace(Tok.getText()))
|
|
|
|
ConsumeToken();
|
|
|
|
}
|
|
|
|
|
2012-06-27 04:39:18 +08:00
|
|
|
if (Tok.is(tok::newline)) {
|
|
|
|
ConsumeToken();
|
2012-08-22 05:15:34 +08:00
|
|
|
// We found a paragraph end. This ends the brief description if
|
2018-05-09 09:00:01 +08:00
|
|
|
// \command or its equivalent was explicitly used.
|
|
|
|
// Stop scanning text because an explicit \paragraph is the
|
2012-08-22 05:15:34 +08:00
|
|
|
// preffered one.
|
2012-07-04 02:10:20 +08:00
|
|
|
if (InBrief)
|
2012-06-28 08:01:41 +08:00
|
|
|
break;
|
2012-08-22 05:15:34 +08:00
|
|
|
// End first paragraph if we found some non-whitespace text.
|
|
|
|
if (InFirstParagraph && !isWhitespace(FirstParagraphOrBrief))
|
|
|
|
InFirstParagraph = false;
|
|
|
|
// End the \\returns paragraph because we found the paragraph end.
|
|
|
|
InReturns = false;
|
2012-06-27 04:39:18 +08:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We didn't handle this token, so just drop it.
|
|
|
|
ConsumeToken();
|
|
|
|
}
|
|
|
|
|
2012-07-21 01:01:34 +08:00
|
|
|
cleanupBrief(FirstParagraphOrBrief);
|
|
|
|
if (!FirstParagraphOrBrief.empty())
|
|
|
|
return FirstParagraphOrBrief;
|
|
|
|
|
|
|
|
cleanupBrief(ReturnsParagraph);
|
|
|
|
return ReturnsParagraph;
|
2012-06-27 04:39:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // end namespace comments
|
|
|
|
} // end namespace clang
|
|
|
|
|
|
|
|
|