From 947a80ecc881c6e63e45be474785b71c1f50f87c Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Sat, 30 Dec 2017 10:56:34 -0800 Subject: [PATCH] [comments] Add a minimalist approach to skip Doxygen comment markers --- src/clang_cursor.cc | 39 +++++++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/src/clang_cursor.cc b/src/clang_cursor.cc index 704566d2..b97243fa 100644 --- a/src/clang_cursor.cc +++ b/src/clang_cursor.cc @@ -198,11 +198,12 @@ optional ClangCursor::get_comments() const { // Get associated comment text. CXString cx_raw = clang_Cursor_getRawCommentText(cx_cursor); - // The first line starts with a comment marker, but the rest needs - // un-indenting. - std::string unindented; + int pad = -1; + std::string ret; for (const char* p = clang_getCString(cx_raw); *p;) { - auto skip = start_column - 1; + // The first line starts with a comment marker, but the rest needs + // un-indenting. + unsigned skip = start_column - 1; for (; skip > 0 && (*p == ' ' || *p == '\t'); p++) skip--; const char* q = p; @@ -210,11 +211,37 @@ optional ClangCursor::get_comments() const { q++; if (*q) q++; - unindented.insert(unindented.end(), p, q); + // A minimalist approach to skip Doxygen comment markers. + // See https://www.stack.nl/~dimitri/doxygen/manual/docblocks.html + if (pad < 0) { + // First line, detect the length of comment marker and put into |pad| + const char* begin = p; + while (*p == '/' || *p == '*') + p++; + if (*p == '<' || *p == '!') + p++; + if (*p == ' ') + p++; + pad = int(p - begin); + } else { + // Other lines, skip |pad| bytes + int prefix = pad; + while (prefix > 0 && + (*p == ' ' || *p == '/' || *p == '*' || *p == '<' || *p == '!')) + prefix--, p++; + } + ret.insert(ret.end(), p, q); p = q; } clang_disposeString(cx_raw); - return unindented; + while (ret.size() && isspace(ret.back())) + ret.pop_back(); + if (ret.size() >= 2 && ret.compare(ret.size() - 2, 2, "*/") == 0) { + ret.resize(ret.size() - 2); + while (ret.size() && isspace(ret.back())) + ret.pop_back(); + } + return ret; } std::string ClangCursor::ToString() const {