[SourceKit] Stop printing normal comments in clang generated interface

Generated interfaces for Clang modules used to try printing normal
comments between decls extracted from the header text. That was because
doc-comment was not common in C/ObjC headers. But mainly because of
"import as member feature" Clang decls aren't printed in the order as
they appear in the header file, the logic determinig which comment
belongs to which decl was not working property. We've decided to remove
that feature and only print the proper doc-comments as it has been
getting common.

rdar://93731287
This commit is contained in:
Rintaro Ishizaki
2023-11-06 22:35:17 -08:00
parent 5d6ce704dd
commit c9785d955d
30 changed files with 4245 additions and 5127 deletions

View File

@@ -44,108 +44,6 @@
using namespace swift;
namespace {
/// Prints regular comments from clang module headers.
class ClangCommentPrinter : public ASTPrinter {
public:
ClangCommentPrinter(ASTPrinter &OtherPrinter, ClangModuleLoader &ClangLoader)
: OtherPrinter(OtherPrinter),
ClangLoader(ClangLoader) {}
private:
void printDeclPre(const Decl *D,
std::optional<BracketOptions> Bracket) override;
void printDeclPost(const Decl *D,
std::optional<BracketOptions> Bracket) override;
void avoidPrintDeclPost(const Decl *D) override;
// Forwarding implementations.
void printText(StringRef Text) override {
return OtherPrinter.printText(Text);
}
void printDeclLoc(const Decl *D) override {
return OtherPrinter.printDeclLoc(D);
}
void printDeclNameEndLoc(const Decl *D) override {
return OtherPrinter.printDeclNameEndLoc(D);
}
void printDeclNameOrSignatureEndLoc(const Decl *D) override {
return OtherPrinter.printDeclNameOrSignatureEndLoc(D);
}
void printTypePre(const TypeLoc &TL) override {
return OtherPrinter.printTypePre(TL);
}
void printTypePost(const TypeLoc &TL) override {
return OtherPrinter.printTypePost(TL);
}
void printTypeRef(Type T, const TypeDecl *TD, Identifier Name,
PrintNameContext NameContext) override {
return OtherPrinter.printTypeRef(T, TD, Name, NameContext);
}
void printModuleRef(ModuleEntity Mod, Identifier Name) override {
return OtherPrinter.printModuleRef(Mod, Name);
}
void
printSynthesizedExtensionPre(const ExtensionDecl *ED,
TypeOrExtensionDecl Target,
std::optional<BracketOptions> Bracket) override {
return OtherPrinter.printSynthesizedExtensionPre(ED, Target, Bracket);
}
void printSynthesizedExtensionPost(
const ExtensionDecl *ED, TypeOrExtensionDecl Target,
std::optional<BracketOptions> Bracket) override {
return OtherPrinter.printSynthesizedExtensionPost(ED, Target, Bracket);
}
void printStructurePre(PrintStructureKind Kind, const Decl *D) override {
return OtherPrinter.printStructurePre(Kind, D);
}
void printStructurePost(PrintStructureKind Kind, const Decl *D) override {
return OtherPrinter.printStructurePost(Kind, D);
}
void printNamePre(PrintNameContext Context) override {
return OtherPrinter.printNamePre(Context);
}
void printNamePost(PrintNameContext Context) override {
return OtherPrinter.printNamePost(Context);
}
// Prints regular comments of the header the clang node comes from, until
// the location of the node. Keeps track of the comments that were printed
// from the file and resumes printing for the next node from the same file.
// This expects to get passed clang nodes in source-order (at least within the
// same header).
void printCommentsUntil(ClangNode Node);
void printComment(StringRef Text, unsigned StartLocCol);
bool isDocumentationComment(clang::SourceLocation CommentLoc,
ClangNode Node) const;
unsigned getResumeOffset(clang::FileID FID) const {
auto OffsI = ResumeOffsets.find(FID);
if (OffsI != ResumeOffsets.end())
return OffsI->second;
return 0;
}
void setResumeOffset(clang::FileID FID, unsigned Offset) {
ResumeOffsets[FID] = Offset;
}
bool shouldPrintNewLineBefore(ClangNode Node) const;
void updateLastEntityLine(clang::SourceLocation Loc);
void updateLastEntityLine(clang::FileID FID, unsigned LineNo);
ASTPrinter &OtherPrinter;
ClangModuleLoader &ClangLoader;
llvm::DenseMap<clang::FileID, unsigned> ResumeOffsets;
SmallVector<StringRef, 2> PendingComments;
llvm::DenseMap<clang::FileID, unsigned> LastEntityLines;
};
} // unnamed namespace
static const clang::Module *
getUnderlyingClangModuleForImport(ImportDecl *Import) {
if (auto *ClangMod = Import->getClangModule())
@@ -761,14 +659,8 @@ void swift::ide::printModuleInterface(
if (GroupNames.empty())
std::stable_sort(SwiftDecls.begin(), SwiftDecls.end(), compareSwiftDecls);
ASTPrinter *PrinterToUse = &Printer;
ClangCommentPrinter RegularCommentPrinter(Printer, Importer);
if (Options.PrintRegularClangComments)
PrinterToUse = &RegularCommentPrinter;
auto PrintDecl = [&](Decl *D) {
return printModuleInterfaceDecl(D, *PrinterToUse, AdjustedOptions,
return printModuleInterfaceDecl(D, Printer, AdjustedOptions,
PrintSynthesizedExtensions);
};
@@ -808,7 +700,7 @@ void swift::ide::printModuleInterface(
// also print the decls from any underscored Swift cross-import overlays it
// is the underlying module of, transitively.
if (GroupNames.empty()) {
printCrossImportOverlays(TargetMod, SwiftContext, *PrinterToUse,
printCrossImportOverlays(TargetMod, SwiftContext, Printer,
AdjustedOptions, PrintSynthesizedExtensions);
}
}
@@ -911,18 +803,11 @@ void swift::ide::printHeaderInterface(
getEffectiveClangNode(RHS).getLocation());
});
ASTPrinter *PrinterToUse = &Printer;
ClangCommentPrinter RegularCommentPrinter(Printer, Importer);
if (Options.PrintRegularClangComments)
PrinterToUse = &RegularCommentPrinter;
for (auto *D : ClangDecls) {
// Even though the corresponding clang decl should be top-level, its
// equivalent Swift decl may not be. E.g. a top-level function may be mapped
// to a property accessor in Swift.
D = getTopLevelDecl(D);
ASTPrinter &Printer = *PrinterToUse;
if (!AdjustedOptions.shouldPrint(D)) {
Printer.callAvoidPrintDeclPost(D);
continue;
@@ -932,215 +817,6 @@ void swift::ide::printHeaderInterface(
}
}
void ClangCommentPrinter::avoidPrintDeclPost(const Decl *D) {
auto CD = D->getClangDecl();
if (!CD)
return;
const auto &Ctx = ClangLoader.getClangASTContext();
const auto &SM = Ctx.getSourceManager();
auto EndLoc = CD->getSourceRange().getEnd();
if (EndLoc.isInvalid())
return;
clang::FileID FID = SM.getFileID(EndLoc);
if (FID.isInvalid())
return;
auto Loc = EndLoc;
for (unsigned Line = SM.getSpellingLineNumber(EndLoc);
Loc.isValid() && SM.getSpellingLineNumber(Loc) == Line;
Loc = Loc.getLocWithOffset(1));
if (Loc.isInvalid())
return;
if (SM.getFileOffset(Loc) > getResumeOffset(FID))
setResumeOffset(FID, SM.getFileOffset(Loc));
}
void ClangCommentPrinter::printDeclPre(const Decl *D,
std::optional<BracketOptions> Bracket) {
// Skip parameters, since we do not gracefully handle nested declarations on a
// single line.
// FIXME: we should fix that, since it also affects struct members, etc.
if (!isa<ParamDecl>(D)) {
if (auto ClangN = swift::ide::getEffectiveClangNode(D)) {
printCommentsUntil(ClangN);
if (shouldPrintNewLineBefore(ClangN)) {
*this << "\n";
printIndent();
}
updateLastEntityLine(ClangN.getSourceRange().getBegin());
}
}
return OtherPrinter.printDeclPre(D, Bracket);
}
void ClangCommentPrinter::printDeclPost(const Decl *D,
std::optional<BracketOptions> Bracket) {
OtherPrinter.printDeclPost(D, Bracket);
// Skip parameters; see printDeclPre().
if (isa<ParamDecl>(D))
return;
for (auto CommentText : PendingComments) {
*this << " " << unicode::sanitizeUTF8(CommentText);
}
PendingComments.clear();
if (auto ClangN = swift::ide::getEffectiveClangNode(D))
updateLastEntityLine(ClangN.getSourceRange().getEnd());
}
void ClangCommentPrinter::printCommentsUntil(ClangNode Node) {
const auto &Ctx = ClangLoader.getClangASTContext();
const auto &SM = Ctx.getSourceManager();
clang::SourceLocation NodeLoc =
SM.getFileLoc(Node.getSourceRange().getBegin());
if (NodeLoc.isInvalid())
return;
unsigned NodeLineNo = SM.getSpellingLineNumber(NodeLoc);
clang::FileID FID = SM.getFileID(NodeLoc);
if (FID.isInvalid())
return;
clang::SourceLocation FileLoc = SM.getLocForStartOfFile(FID);
StringRef Text = SM.getBufferData(FID);
if (Text.empty())
return;
const char *BufStart = Text.data();
const char *BufPtr = BufStart + getResumeOffset(FID);
const char *BufEnd = BufStart + Text.size();
assert(BufPtr <= BufEnd);
if (BufPtr == BufEnd)
return; // nothing left.
clang::Lexer Lex(FileLoc, Ctx.getLangOpts(), BufStart, BufPtr, BufEnd);
Lex.SetCommentRetentionState(true);
unsigned &LastPrintedLineNo = LastEntityLines[FID];
clang::Token Tok;
do {
BufPtr = Lex.getBufferLocation();
Lex.LexFromRawLexer(Tok);
if (Tok.is(clang::tok::eof))
break;
if (Tok.isNot(clang::tok::comment))
continue;
// Reached a comment.
clang::SourceLocation CommentLoc = Tok.getLocation();
std::pair<clang::FileID, unsigned> LocInfo =
SM.getDecomposedLoc(CommentLoc);
assert(LocInfo.first == FID);
unsigned LineNo = SM.getLineNumber(LocInfo.first, LocInfo.second);
if (LineNo > NodeLineNo)
break; // Comment is past the clang node.
bool IsDocComment = isDocumentationComment(CommentLoc, Node);
// Print out the comment.
StringRef CommentText(BufStart + LocInfo.second, Tok.getLength());
// Check if comment is on same line but after the declaration.
if (SM.isBeforeInTranslationUnit(NodeLoc, Tok.getLocation())) {
if (!IsDocComment)
PendingComments.push_back(CommentText);
continue;
}
if (LastPrintedLineNo && LineNo - LastPrintedLineNo > 1) {
*this << "\n";
printIndent();
}
if (!IsDocComment) {
unsigned StartLocCol = SM.getSpellingColumnNumber(Tok.getLocation());
printComment(CommentText, StartLocCol);
}
LastPrintedLineNo =
SM.getLineNumber(LocInfo.first, LocInfo.second + Tok.getLength());
} while (true);
// Resume printing comments from this point.
setResumeOffset(FID, BufPtr - BufStart);
}
void ClangCommentPrinter::printComment(StringRef RawText, unsigned StartCol) {
unsigned WhitespaceToTrim = StartCol ? StartCol - 1 : 0;
SmallVector<StringRef, 8> Lines;
trimLeadingWhitespaceFromLines(RawText, WhitespaceToTrim, Lines);
for (auto Line : Lines) {
*this << unicode::sanitizeUTF8(Line) << "\n";
printIndent();
}
}
bool ClangCommentPrinter::isDocumentationComment(
clang::SourceLocation CommentLoc, ClangNode Node) const {
const clang::Decl *D = Node.getAsDecl();
if (!D)
return false;
const auto &Ctx = ClangLoader.getClangASTContext();
const auto &SM = Ctx.getSourceManager();
const clang::RawComment *RC = Ctx.getRawCommentForAnyRedecl(D);
if (!RC)
return false;
clang::SourceRange DocRange = RC->getSourceRange();
if (SM.isBeforeInTranslationUnit(CommentLoc, DocRange.getBegin()) ||
SM.isBeforeInTranslationUnit(DocRange.getEnd(), CommentLoc))
return false;
return true;
}
bool ClangCommentPrinter::shouldPrintNewLineBefore(ClangNode Node) const {
assert(Node);
const auto &Ctx = ClangLoader.getClangASTContext();
const auto &SM = Ctx.getSourceManager();
clang::SourceLocation NodeLoc =
SM.getFileLoc(Node.getSourceRange().getBegin());
if (NodeLoc.isInvalid())
return false;
unsigned NodeLineNo = SM.getSpellingLineNumber(NodeLoc);
clang::FileID FID = SM.getFileID(NodeLoc);
if (FID.isInvalid())
return false;
unsigned LastEntiyLine = 0;
auto It = LastEntityLines.find(FID);
if (It != LastEntityLines.end())
LastEntiyLine = It->second;
return (NodeLineNo > LastEntiyLine) && NodeLineNo - LastEntiyLine > 1;
}
void ClangCommentPrinter::updateLastEntityLine(clang::SourceLocation Loc) {
if (Loc.isInvalid())
return;
const auto &Ctx = ClangLoader.getClangASTContext();
const auto &SM = Ctx.getSourceManager();
unsigned LineNo = SM.getSpellingLineNumber(Loc);
clang::FileID FID = SM.getFileID(Loc);
if (FID.isInvalid())
return;
updateLastEntityLine(FID, LineNo);
}
void ClangCommentPrinter::updateLastEntityLine(clang::FileID FID,
unsigned LineNo) {
assert(!FID.isInvalid());
unsigned &LastEntiyLine = LastEntityLines[FID];
if (LineNo > LastEntiyLine)
LastEntiyLine = LineNo;
}
void swift::ide::printSymbolicSwiftClangModuleInterface(
ModuleDecl *M, ASTPrinter &Printer, const clang::Module *clangModule) {
std::string headerComment;
@@ -1154,7 +830,6 @@ void swift::ide::printSymbolicSwiftClangModuleInterface(
auto popts =
PrintOptions::printModuleInterface(/*printFullConvention=*/false);
popts.PrintDocumentationComments = false;
popts.PrintRegularClangComments = false;
popts.SkipInlineCXXNamespace = true;
auto &SwiftContext = M->getTopLevelModule()->getASTContext();