Skip to content

Commit e265c12

Browse files
authoredNov 10, 2021
Merge pull request #2277 from guwirth/unamed-enums
UndocumentedApi: support unnamed-enum
2 parents 61a419d + 8d0bccd commit e265c12

File tree

3 files changed

+75
-12
lines changed

3 files changed

+75
-12
lines changed
 

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

+37-10
Original file line numberDiff line numberDiff line change
@@ -795,24 +795,51 @@ private void visitAliasDeclaration(AstNode aliasDeclNode) {
795795
}
796796
}
797797

798-
private void visitEnumSpecifier(AstNode enumSpecifierNode) {
799-
var enumIdNode = enumSpecifierNode.getFirstDescendant(GenericTokenType.IDENTIFIER);
798+
private static List<Token> getTypedefInlineComment(AstNode typedef) {
799+
var commentTokens = new ArrayList<Token>();
800+
var node = typedef.getFirstAncestor(CxxGrammarImpl.declaration);
801+
node = node.getNextAstNode();
802+
803+
// search for first child with a comment
804+
while (node != null) {
805+
for (var trivia : node.getToken().getTrivia()) {
806+
if (trivia.isComment()) {
807+
commentTokens.add(trivia.getToken());
808+
return commentTokens;
809+
}
810+
}
811+
node = node.getFirstChild();
812+
}
800813

801-
String enumId = (enumIdNode == null) ? UNNAMED_ENUM_ID : enumIdNode.getTokenValue();
814+
return commentTokens;
815+
}
802816

817+
private void visitEnumSpecifier(AstNode enumSpecifierNode) {
803818
if (!isPublicApiMember(enumSpecifierNode)) {
804-
// not in public API
805-
return;
819+
return; // not in public API
806820
}
807821

808-
// deal with typedef enum: documentation is on typedef node
809-
AstNode docNode = getTypedefNode(enumSpecifierNode);
822+
var docNode = getTypedefNode(enumSpecifierNode);
823+
AstNode idNode;
810824
if (docNode == null) {
811-
docNode = enumSpecifierNode;
825+
// enum ...
826+
idNode = enumSpecifierNode.getFirstDescendant(CxxGrammarImpl.enumHeadName);
827+
if (idNode != null) {
828+
visitPublicApi(enumSpecifierNode, idNode.getTokenValue(), getBlockDocumentation(enumSpecifierNode));
829+
}
830+
} else {
831+
// typedef enum ...
832+
var declaration = enumSpecifierNode.getFirstAncestor(CxxGrammarImpl.declaration);
833+
idNode = declaration.getFirstDescendant(CxxGrammarImpl.declaratorId);
834+
if (idNode != null) {
835+
List<Token> comments = getBlockDocumentation(docNode);
836+
if (comments.isEmpty()) { // documentation may be inlined
837+
comments = getTypedefInlineComment(docNode);
838+
}
839+
visitPublicApi(enumSpecifierNode, idNode.getTokenValue(), comments);
840+
}
812841
}
813842

814-
visitPublicApi(enumSpecifierNode, enumId, getBlockDocumentation(docNode));
815-
816843
// deal with enumeration values
817844
AstNode enumeratorList = enumSpecifierNode.getFirstDescendant(CxxGrammarImpl.enumeratorList);
818845

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public void unnamed_class() throws IOException {
9898

9999
@Test
100100
public void unnamed_enum() throws IOException {
101-
assertThat(verifyPublicApiOfFile("src/test/resources/metrics/unnamed_enum.h")).isEqualTo(tuple(1, 1));
101+
assertThat(verifyPublicApiOfFile("src/test/resources/metrics/unnamed_enum.h")).isEqualTo(tuple(8, 0));
102102
}
103103

104104
@Test
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,39 @@
1-
enum /*anonymous enum*/ {
1+
// enum ...
2+
3+
/** doc */
4+
enum named_enum_1 {
5+
};
6+
7+
enum /*unnamed-enum*/ {
8+
};
9+
10+
/** doc */
11+
enum named_enum_2 : int {
12+
};
13+
14+
enum /*unnamed-enum*/ : int {
215
};
316

17+
// typedef ...
18+
19+
/** doc */
20+
typedef enum named_enum_3 {
21+
} type_name_1;
22+
23+
/** doc */
24+
typedef enum named_enum_4 : int {
25+
} type_name_2;
26+
27+
/** doc */
28+
typedef enum /*unnamed-enum*/ {
29+
} type_name_3;
30+
31+
typedef enum /*unnamed-enum*/ {
32+
} type_name_4; ///< doc
33+
34+
/** doc */
35+
typedef enum /*unnamed-enum*/ : int {
36+
} type_name_5;
37+
38+
typedef enum /*unnamed-enum*/ : int {
39+
} type_name_6; ///< doc

0 commit comments

Comments
 (0)
Please sign in to comment.