Skip to content

Commit c59843e

Browse files
authored
Merge pull request #2163 from guwirth/fix-2161
public API detection: improved detection of comments at the end of last line
2 parents c1d08bc + c819a50 commit c59843e

File tree

3 files changed

+60
-45
lines changed

3 files changed

+60
-45
lines changed

cxx-squid/src/main/java/org/sonar/cxx/visitors/AbstractCxxPublicApiVisitor.java

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -227,22 +227,19 @@ private static String getOperatorId(AstNode operatorFunctionId) {
227227
private static List<Token> getDeclaratorInlineComment(AstNode declarator) {
228228
List<Token> comments;
229229

230-
// inline comments are attached to the next AST node (not sibling,
231-
// because the last attribute inline comment is attached to the next
232-
// node of the parent)
230+
// inline comments are attached to the next AST node (not sibling, because the last attribute inline comment
231+
// is attached to the next node of the parent)
233232
var next = declarator.getNextAstNode();
234233

235-
// inline documentation may be on the next definition token
236-
// or next curly brace
234+
// inline documentation may be on the next definition token or next curly brace
237235
if (next != null) {
238236
// discard COMMA and SEMICOLON
239237
if (next.getToken().getType().equals(CxxPunctuator.COMMA)
240238
|| next.getToken().getType().equals(CxxPunctuator.SEMICOLON)) {
241239
next = next.getNextAstNode();
242240
}
243241

244-
comments = getInlineDocumentation(next.getToken(),
245-
declarator.getTokenLine());
242+
comments = getInlineDocumentation(next.getToken());
246243
} else {
247244
// could happen on parse error ?
248245
comments = new ArrayList<>();
@@ -309,21 +306,18 @@ private static boolean isPublicApiMember(AstNode node) {
309306
}
310307

311308
/**
312-
* Check if inline Doxygen documentation is attached to the given token at specified line
309+
* Check if inline Doxygen documentation is attached to the end of the line
313310
*
314311
* @param token the token to inspect
315-
* @param line line of the inlined documentation
316312
* @return true if documentation is found for specified line, false otherwise
317313
*/
318-
private static List<Token> getInlineDocumentation(Token token, int line) {
314+
private static List<Token> getInlineDocumentation(Token token) {
319315
var comments = new ArrayList<Token>();
320316

321317
for (var trivia : token.getTrivia()) {
322318
if (trivia.isComment()) {
323319
var triviaToken = trivia.getToken();
324-
if ((triviaToken != null)
325-
&& (triviaToken.getLine() == line)
326-
&& (isDoxygenInlineComment(triviaToken.getValue()))) {
320+
if ((triviaToken != null) && (isDoxygenInlineComment(triviaToken.getValue()))) {
327321
comments.add(triviaToken);
328322
}
329323
}
@@ -806,11 +800,9 @@ private void visitAliasDeclaration(AstNode aliasDeclNode) {
806800
}
807801

808802
private void visitEnumSpecifier(AstNode enumSpecifierNode) {
809-
var enumIdNode = enumSpecifierNode.getFirstDescendant(
810-
GenericTokenType.IDENTIFIER);
803+
var enumIdNode = enumSpecifierNode.getFirstDescendant(GenericTokenType.IDENTIFIER);
811804

812-
String enumId = (enumIdNode == null)
813-
? UNNAMED_ENUM_ID : enumIdNode.getTokenValue();
805+
String enumId = (enumIdNode == null) ? UNNAMED_ENUM_ID : enumIdNode.getTokenValue();
814806

815807
if (!isPublicApiMember(enumSpecifierNode)) {
816808
// not in public API
@@ -823,17 +815,13 @@ private void visitEnumSpecifier(AstNode enumSpecifierNode) {
823815
docNode = enumSpecifierNode;
824816
}
825817

826-
visitPublicApi(
827-
enumSpecifierNode, enumId,
828-
getBlockDocumentation(docNode));
818+
visitPublicApi(enumSpecifierNode, enumId,getBlockDocumentation(docNode));
829819

830820
// deal with enumeration values
831-
AstNode enumeratorList = enumSpecifierNode
832-
.getFirstDescendant(CxxGrammarImpl.enumeratorList);
821+
AstNode enumeratorList = enumSpecifierNode.getFirstDescendant(CxxGrammarImpl.enumeratorList);
833822

834823
if (enumeratorList != null) {
835-
for (var definition : enumeratorList
836-
.getChildren(CxxGrammarImpl.enumeratorDefinition)) {
824+
for (var definition : enumeratorList.getChildren(CxxGrammarImpl.enumeratorDefinition)) {
837825

838826
// look for block documentation
839827
List<Token> comments = getBlockDocumentation(definition);
@@ -842,24 +830,18 @@ private void visitEnumSpecifier(AstNode enumSpecifierNode) {
842830
if (comments.isEmpty()) {
843831
var next = definition.getNextAstNode();
844832

845-
// inline documentation may be on the next definition token
846-
// or next curly brace
833+
// inline documentation may be on the next definition token or next curly brace
847834
if (next != null) {
848835
// discard COMMA
849836
if (next.getToken().getType().equals(CxxPunctuator.COMMA)) {
850837
next = next.getNextAstNode();
851838
}
852839

853-
comments = getInlineDocumentation(next.getToken(),
854-
definition.getTokenLine());
840+
comments = getInlineDocumentation(next.getToken());
855841
}
856842
}
857843

858-
visitPublicApi(
859-
definition,
860-
definition.getFirstDescendant(
861-
GenericTokenType.IDENTIFIER).getTokenValue(),
862-
comments);
844+
visitPublicApi(definition, definition.getFirstDescendant( GenericTokenType.IDENTIFIER).getTokenValue(), comments);
863845
}
864846
}
865847
}

cxx-squid/src/test/java/org/sonar/cxx/visitors/CxxPublicApiVisitorTest.java

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,38 +67,43 @@ public void test_no_matching_suffix() throws IOException {
6767

6868
@Test
6969
public void doxygen_example() throws IOException {
70-
assertThat(testFile("src/test/resources/metrics/doxygen_example.h")).isEqualTo(tuple(13, 0));
70+
assertThat(verifyPublicApiOfFile("src/test/resources/metrics/doxygen_example.h")).isEqualTo(tuple(13, 0));
7171
}
7272

7373
@Test
7474
public void to_delete() throws IOException {
75-
assertThat(testFile("src/test/resources/metrics/public_api.h")).isEqualTo(tuple(47, 0));
75+
assertThat(verifyPublicApiOfFile("src/test/resources/metrics/public_api.h")).isEqualTo(tuple(47, 0));
7676

7777
}
7878

7979
@Test
8080
public void no_doc() throws IOException {
81-
assertThat(testFile("src/test/resources/metrics/no_doc.h")).isEqualTo(tuple(22, 22));
81+
assertThat(verifyPublicApiOfFile("src/test/resources/metrics/no_doc.h")).isEqualTo(tuple(22, 22));
8282
}
8383

8484
@Test
8585
public void template() throws IOException {
86-
assertThat(testFile("src/test/resources/metrics/template.h")).isEqualTo(tuple(14, 4));
86+
assertThat(verifyPublicApiOfFile("src/test/resources/metrics/template.h")).isEqualTo(tuple(14, 4));
8787
}
8888

8989
@Test
9090
public void alias_function_template() throws IOException {
91-
assertThat(testFile("src/test/resources/metrics/alias_in_template_func.h")).isEqualTo(tuple(4, 3));
91+
assertThat(verifyPublicApiOfFile("src/test/resources/metrics/alias_in_template_func.h")).isEqualTo(tuple(4, 3));
9292
}
9393

9494
@Test
9595
public void unnamed_class() throws IOException {
96-
assertThat(testFile("src/test/resources/metrics/unnamed_class.h")).isEqualTo(tuple(3, 1));
96+
assertThat(verifyPublicApiOfFile("src/test/resources/metrics/unnamed_class.h")).isEqualTo(tuple(3, 1));
9797
}
9898

9999
@Test
100100
public void unnamed_enum() throws IOException {
101-
assertThat(testFile("src/test/resources/metrics/unnamed_enum.h")).isEqualTo(tuple(1, 1));
101+
assertThat(verifyPublicApiOfFile("src/test/resources/metrics/unnamed_enum.h")).isEqualTo(tuple(1, 1));
102+
}
103+
104+
@Test
105+
public void multiline() throws IOException {
106+
assertThat(verifyPublicApiOfFile("src/test/resources/metrics/multiline.h")).isEqualTo(tuple(9, 0));
102107
}
103108

104109
@Test
@@ -199,13 +204,12 @@ public void public_api() throws UnsupportedEncodingException, IOException {
199204
}
200205

201206
/**
202-
* Check that CxxPublicApiVisitor correctly counts API for given file.
207+
* Check that CxxPublicApiVisitor correctly counts public API for given file.
203208
*
204-
* @param fileName the file to use for test
205-
* @param expectedApi expected number of API
206-
* @param expectedUndoc expected number of undocumented API
209+
* @param fileName file to verify
210+
* @return tuple(found public API, thereof undocumented public API)
207211
*/
208-
private Tuple testFile(String fileName)
212+
private Tuple verifyPublicApiOfFile(String fileName)
209213
throws UnsupportedEncodingException, IOException {
210214

211215
var squidConfig = new CxxSquidConfiguration();
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/* multi-line definitions/declarations and comments at the end of last line */
2+
3+
/**
4+
docu MyEnum (issue #2161)
5+
*/
6+
typedef enum {
7+
A = 0, ///< docu A
8+
B =
9+
1, ///< docu B
10+
} MyEnum;
11+
12+
/**
13+
docu MyClass
14+
*/
15+
class MyClass {
16+
public:
17+
void method1() = 0; ///< docu method1
18+
void method2() =
19+
0; ///< docu method2
20+
};
21+
22+
/**
23+
docu MyType
24+
*/
25+
struct MyType {
26+
int var1 = 1; ///< docu var1
27+
int var2 =
28+
2; ///< docu var2
29+
};

0 commit comments

Comments
 (0)