Skip to content

Commit fe03a82

Browse files
Copilotstrimo378
andcommitted
fix: __attribute__ of declarator is missing from AST (issues #706 and #602)
Move `mark()` call to before `__attribute_decl_seq()` in `GNUCSourceParser.consumePointerOperators()` so that when no pointer operator follows an attribute, `backup(mark)` correctly restores the token stream to before the attribute. Previously, the mark was set *after* consuming the attribute, so the attribute was permanently lost. Also update `startOffset` to use `last.getOffset()` (the offset of the `*` token) instead of `mark.getOffset()` to preserve correct pointer operator location information. Agent-Logs-Url: https://github.com/emmtrix/cdt/sessions/64cb901c-b4db-40ca-bc5e-49f7c8ae83a2 Co-authored-by: strimo378 <59825937+strimo378@users.noreply.github.com>
1 parent acd4556 commit fe03a82

2 files changed

Lines changed: 18 additions & 2 deletions

File tree

core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2CPPAttributeTests.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,4 +642,19 @@ public void testMixedAttributeSpecifiers() throws Exception {
642642
assertSame(parent1, parent3);
643643
assertSame(parent1, parent4);
644644
}
645+
646+
// int target;
647+
// extern int other, __attribute__((alias("target"))) error_decl;
648+
@Test
649+
public void testGCCAttributeBeforeDeclaratorInList_bug706() throws Exception {
650+
IASTTranslationUnit tu = parseAndCheckBindings(getAboveComment(), ParserLanguage.C, ScannerKind.GNU);
651+
checkAttributeRelations(getAttributeSpecifiers(tu), IASTDeclarator.class);
652+
}
653+
654+
// extern int ( __attribute__((__artificial__)) func)();
655+
@Test
656+
public void testGCCAttributeInNestedDeclarator_bug602() throws Exception {
657+
IASTTranslationUnit tu = parseAndCheckBindings(getAboveComment(), ParserLanguage.C, ScannerKind.GNU);
658+
checkAttributeRelations(getAttributeSpecifiers(tu), IASTDeclarator.class);
659+
}
645660
}

core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/GNUCSourceParser.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -869,10 +869,11 @@ protected IASTTypeId typeId(DeclarationOptions option) throws EndOfFileException
869869
protected void consumePointerOperators(List<IASTPointerOperator> pointerOps)
870870
throws EndOfFileException, BacktrackException {
871871
for (;;) {
872+
IToken mark = mark();
873+
872874
// __attribute__ in-between pointers
873875
__attribute_decl_seq(supportAttributeSpecifiers, false);
874876

875-
IToken mark = mark();
876877
IToken last = null;
877878

878879
boolean isConst = false, isVolatile = false, isRestrict = false;
@@ -883,7 +884,7 @@ protected void consumePointerOperators(List<IASTPointerOperator> pointerOps)
883884
}
884885

885886
last = consume();
886-
int startOffset = mark.getOffset();
887+
int startOffset = last.getOffset();
887888
for (;;) {
888889
IToken t = LA(1);
889890
switch (LT(1)) {

0 commit comments

Comments
 (0)