Skip to content

Commit

Permalink
Merge pull request #2277 from guwirth/unamed-enums
Browse files Browse the repository at this point in the history
UndocumentedApi: support unnamed-enum
  • Loading branch information
guwirth authored Nov 10, 2021
2 parents 61a419d + 8d0bccd commit e265c12
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -795,24 +795,51 @@ private void visitAliasDeclaration(AstNode aliasDeclNode) {
}
}

private void visitEnumSpecifier(AstNode enumSpecifierNode) {
var enumIdNode = enumSpecifierNode.getFirstDescendant(GenericTokenType.IDENTIFIER);
private static List<Token> getTypedefInlineComment(AstNode typedef) {
var commentTokens = new ArrayList<Token>();
var node = typedef.getFirstAncestor(CxxGrammarImpl.declaration);
node = node.getNextAstNode();

// search for first child with a comment
while (node != null) {
for (var trivia : node.getToken().getTrivia()) {
if (trivia.isComment()) {
commentTokens.add(trivia.getToken());
return commentTokens;
}
}
node = node.getFirstChild();
}

String enumId = (enumIdNode == null) ? UNNAMED_ENUM_ID : enumIdNode.getTokenValue();
return commentTokens;
}

private void visitEnumSpecifier(AstNode enumSpecifierNode) {
if (!isPublicApiMember(enumSpecifierNode)) {
// not in public API
return;
return; // not in public API
}

// deal with typedef enum: documentation is on typedef node
AstNode docNode = getTypedefNode(enumSpecifierNode);
var docNode = getTypedefNode(enumSpecifierNode);
AstNode idNode;
if (docNode == null) {
docNode = enumSpecifierNode;
// enum ...
idNode = enumSpecifierNode.getFirstDescendant(CxxGrammarImpl.enumHeadName);
if (idNode != null) {
visitPublicApi(enumSpecifierNode, idNode.getTokenValue(), getBlockDocumentation(enumSpecifierNode));
}
} else {
// typedef enum ...
var declaration = enumSpecifierNode.getFirstAncestor(CxxGrammarImpl.declaration);
idNode = declaration.getFirstDescendant(CxxGrammarImpl.declaratorId);
if (idNode != null) {
List<Token> comments = getBlockDocumentation(docNode);
if (comments.isEmpty()) { // documentation may be inlined
comments = getTypedefInlineComment(docNode);
}
visitPublicApi(enumSpecifierNode, idNode.getTokenValue(), comments);
}
}

visitPublicApi(enumSpecifierNode, enumId, getBlockDocumentation(docNode));

// deal with enumeration values
AstNode enumeratorList = enumSpecifierNode.getFirstDescendant(CxxGrammarImpl.enumeratorList);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public void unnamed_class() throws IOException {

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

@Test
Expand Down
38 changes: 37 additions & 1 deletion cxx-squid/src/test/resources/metrics/unnamed_enum.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,39 @@
enum /*anonymous enum*/ {
// enum ...

/** doc */
enum named_enum_1 {
};

enum /*unnamed-enum*/ {
};

/** doc */
enum named_enum_2 : int {
};

enum /*unnamed-enum*/ : int {
};

// typedef ...

/** doc */
typedef enum named_enum_3 {
} type_name_1;

/** doc */
typedef enum named_enum_4 : int {
} type_name_2;

/** doc */
typedef enum /*unnamed-enum*/ {
} type_name_3;

typedef enum /*unnamed-enum*/ {
} type_name_4; ///< doc

/** doc */
typedef enum /*unnamed-enum*/ : int {
} type_name_5;

typedef enum /*unnamed-enum*/ : int {
} type_name_6; ///< doc

0 comments on commit e265c12

Please sign in to comment.