Skip to content

Commit 6a7879d

Browse files
ignore embedded doccomment inside initializer list
1 parent e3631cf commit 6a7879d

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed

cppparser/src/parser.y

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ using namespace cppast;
251251
%type <str> optapidecor apidecor apidecortokensq
252252
%type <str> identifier optidentifier numbertype typeidentifier varidentifier optname id name operfuncname funcname
253253
%type <str> templidentifier templqualifiedid
254-
%type <str> doccommentstr
254+
%type <str> doccommentstr optdoccommentstr
255255
%type <str> rshift
256256
%type <str> macrocall
257257
%type <cppEntity> stmt
@@ -675,6 +675,11 @@ doccomment
675675
: doccommentstr [ZZLOG;] { $$ = new cppast::CppDocumentationComment((std::string) $1); }
676676
;
677677

678+
optdoccommentstr
679+
: [ZZLOG;] { $$ = CppToken{nullptr, 0}; }
680+
| doccommentstr [ZZLOG;] { $$ = $1; }
681+
;
682+
678683
doccommentstr
679684
: tknFreeStandingBlockComment [ZZLOG;] { $$ = $1; }
680685
| tknFreeStandingLineComment [ZZLOG;] { $$ = $1; }
@@ -2068,8 +2073,9 @@ exprlist
20682073
$$ = new std::vector<std::unique_ptr<const cppast::CppExpression>>;
20692074
$$->emplace_back($1);
20702075
}
2071-
| exprlist ',' expr %prec COMMA [ZZLOG;] { $1->emplace_back($3); $$ = $1; }
2072-
| doccommentstr exprlist [ZZLOG;] { $$ = $2; }
2076+
| exprlist optdoccommentstr ',' optdoccommentstr expr %prec COMMA [ZZLOG;] { $1->emplace_back($5); $$ = $1; }
2077+
| doccommentstr exprlist [ZZLOG;] { $$ = $2; }
2078+
| exprlist doccommentstr [ZZLOG;] { $$ = $1; }
20732079
;
20742080

20752081
optexprlist

cppparser/test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ set(TEST_SNIPPET_EMBEDDED_TESTS
3737
${CMAKE_CURRENT_LIST_DIR}/unit/disabled-code-test.cpp
3838
${CMAKE_CURRENT_LIST_DIR}/unit/error-handler-test.cpp
3939
${CMAKE_CURRENT_LIST_DIR}/unit/expr-test.cpp
40+
${CMAKE_CURRENT_LIST_DIR}/unit/initializer-list-test.cpp
4041
${CMAKE_CURRENT_LIST_DIR}/unit/namespace-test.cpp
4142
${CMAKE_CURRENT_LIST_DIR}/unit/template-test.cpp
4243
${CMAKE_CURRENT_LIST_DIR}/unit/uniform-init-test.cpp
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright (C) 2022 Satya Das and CppParser contributors
2+
// SPDX-License-Identifier: MIT
3+
4+
#include <catch/catch.hpp>
5+
6+
#include "cppparser/cppparser.h"
7+
8+
#include "embedded-snippet-test-base.h"
9+
10+
#include <string>
11+
12+
class InitializerListTest : public EmbeddedSnippetTestBase
13+
{
14+
protected:
15+
InitializerListTest()
16+
: EmbeddedSnippetTestBase(__FILE__)
17+
{
18+
}
19+
};
20+
21+
TEST_CASE_METHOD(InitializerListTest, "docstr in middle", "[initializerlist]")
22+
{
23+
#if TEST_CASE_SNIPPET_STARTS_FROM_NEXT_LINE
24+
bool boolArray[] = {false,
25+
// IGNORABLE COMMENTS HERE
26+
false};
27+
#endif
28+
auto testSnippet = getTestSnippetParseStream(__LINE__ - 2);
29+
30+
cppparser::CppParser parser;
31+
const auto ast = parser.parseStream(testSnippet.data(), testSnippet.size());
32+
REQUIRE(ast != nullptr);
33+
34+
const auto members = GetAllOwnedEntities(*ast);
35+
REQUIRE(members.size() == 1);
36+
cppast::CppConstVarEPtr var = members[0];
37+
REQUIRE(var);
38+
}

0 commit comments

Comments
 (0)