From 9df3b39e1e80e4b3b9f991f53cc7308b26a8a1d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonathan=20M=C3=BCller?= Date: Wed, 13 Dec 2017 17:30:46 +0100 Subject: [PATCH] Fix comment matching for includes not on top of the file See foonathan/standardese#83. --- src/libclang/libclang_parser.cpp | 4 +++- src/libclang/parse_functions.cpp | 6 +++++- src/libclang/parse_functions.hpp | 2 +- test/cpp_preprocessor.cpp | 16 ++++++++++++++-- 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/libclang/libclang_parser.cpp b/src/libclang/libclang_parser.cpp index e40abe9..14fb260 100644 --- a/src/libclang/libclang_parser.cpp +++ b/src/libclang/libclang_parser.cpp @@ -489,7 +489,9 @@ std::unique_ptr libclang_parser::do_parse(const cpp_entity_index& idx, auto include = cpp_include_directive::build(std::move(include_iter->file), include_iter->kind, detail::get_cursor_name(cur).c_str()); - context.comments.match(*include, include_iter->line); + context.comments.match(*include, include_iter->line, + false); // must not skip comments, + // includes are not reported in order builder.add_child(std::move(include)); ++include_iter; diff --git a/src/libclang/parse_functions.cpp b/src/libclang/parse_functions.cpp index 0b907a2..a4f5a60 100644 --- a/src/libclang/parse_functions.cpp +++ b/src/libclang/parse_functions.cpp @@ -85,13 +85,17 @@ void detail::comment_context::match(cpp_entity& e, const CXCursor& cur) const match(e, line); } -void detail::comment_context::match(cpp_entity& e, unsigned line) const +void detail::comment_context::match(cpp_entity& e, unsigned line, bool skip_comments) const { // find comment + auto save = cur_; while (cur_ != end_ && cur_->line + 1 < line) ++cur_; if (cur_ != end_ && cur_->matches(e, line)) e.set_comment(std::move(cur_++->comment)); + + if (!skip_comments) + cur_ = save; } namespace diff --git a/src/libclang/parse_functions.hpp b/src/libclang/parse_functions.hpp index 2c33b20..da732cd 100644 --- a/src/libclang/parse_functions.hpp +++ b/src/libclang/parse_functions.hpp @@ -48,7 +48,7 @@ namespace cppast // must be called for entities that want an associated comment // must be called *BEFORE* the children are added void match(cpp_entity& e, const CXCursor& cur) const; - void match(cpp_entity& e, unsigned line) const; + void match(cpp_entity& e, unsigned line, bool skip_comments = true) const; private: mutable pp_doc_comment* cur_; diff --git a/test/cpp_preprocessor.cpp b/test/cpp_preprocessor.cpp index e0695f6..e46c7eb 100644 --- a/test/cpp_preprocessor.cpp +++ b/test/cpp_preprocessor.cpp @@ -279,6 +279,10 @@ g(h) i */ using i = int; +/// cstddef +/// cstddef +#include + /// j /// j template @@ -300,7 +304,15 @@ void j(); return true; }); + auto add = 0u; for (auto& comment : file->unmatched_comments()) - REQUIRE(comment.content == "u"); - REQUIRE((file->unmatched_comments().size() == 3u)); + { + if (comment.content == "cstddef\ncstddef") + // happens if include parsing is not supported + // error is still going to be detected because if it is supported, the entity will be matched above + add = 1u; + else + REQUIRE(comment.content == "u"); + } + REQUIRE((file->unmatched_comments().size() == 3u + add)); }