Skip to content

Commit 3656eab

Browse files
Copilotstrimo378
andauthored
Add inline/goto qualifier support to IASTASMDeclaration and parser
Agent-Logs-Url: https://github.com/emmtrix/cdt/sessions/9c4b15e3-04dd-48a2-8514-3639dc110f3f Co-authored-by: strimo378 <59825937+strimo378@users.noreply.github.com>
1 parent acd4556 commit 3656eab

6 files changed

Lines changed: 207 additions & 4 deletions

File tree

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package org.eclipse.cdt.core.parser.tests.ast2;
1616

1717
import static org.junit.jupiter.api.Assertions.assertEquals;
18+
import static org.junit.jupiter.api.Assertions.assertFalse;
1819
import static org.junit.jupiter.api.Assertions.assertTrue;
1920

2021
import java.io.StringWriter;
@@ -507,4 +508,50 @@ public void testIntegerOverflowBuiltin_bug271() throws Exception {
507508
parseGCC(code);
508509
parseGPP(code);
509510
}
511+
512+
@Test
513+
public void testAsmInlineGotoQualifiers() throws Exception {
514+
// __asm__ inline ("nop");
515+
IASTDeclaration[] decls = parseGCC("__asm__ inline (\"nop\");").getDeclarations();
516+
IASTASMDeclaration asmDecl = (IASTASMDeclaration) decls[0];
517+
assertEquals("\"nop\"", asmDecl.getAssembly());
518+
assertTrue(asmDecl.isInline());
519+
assertFalse(asmDecl.isVolatile());
520+
assertFalse(asmDecl.isGoto());
521+
522+
// __asm__ goto ("nop");
523+
decls = parseGCC("__asm__ goto (\"nop\");").getDeclarations();
524+
asmDecl = (IASTASMDeclaration) decls[0];
525+
assertEquals("\"nop\"", asmDecl.getAssembly());
526+
assertTrue(asmDecl.isGoto());
527+
assertFalse(asmDecl.isVolatile());
528+
assertFalse(asmDecl.isInline());
529+
530+
// asm volatile inline goto ("nop"); - multiple qualifiers in any order
531+
decls = parseGCC("asm volatile inline goto (\"nop\");").getDeclarations();
532+
asmDecl = (IASTASMDeclaration) decls[0];
533+
assertTrue(asmDecl.isVolatile());
534+
assertTrue(asmDecl.isInline());
535+
assertTrue(asmDecl.isGoto());
536+
537+
// CPP variants
538+
decls = parseGPP("__asm__ inline (\"nop\");").getDeclarations();
539+
asmDecl = (IASTASMDeclaration) decls[0];
540+
assertTrue(asmDecl.isInline());
541+
assertFalse(asmDecl.isVolatile());
542+
assertFalse(asmDecl.isGoto());
543+
544+
decls = parseGPP("__asm__ goto (\"nop\");").getDeclarations();
545+
asmDecl = (IASTASMDeclaration) decls[0];
546+
assertTrue(asmDecl.isGoto());
547+
assertFalse(asmDecl.isVolatile());
548+
assertFalse(asmDecl.isInline());
549+
550+
// volatile qualifier (existing) is still stored correctly
551+
decls = parseGCC("asm volatile (\"nop\");").getDeclarations();
552+
asmDecl = (IASTASMDeclaration) decls[0];
553+
assertTrue(asmDecl.isVolatile());
554+
assertFalse(asmDecl.isInline());
555+
assertFalse(asmDecl.isGoto());
556+
}
510557
}

core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IASTASMDeclaration.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,48 @@ public interface IASTASMDeclaration extends IASTDeclaration {
3434
*/
3535
public void setAssembly(String assembly);
3636

37+
/**
38+
* Returns whether the <code>volatile</code> qualifier was present.
39+
*
40+
* @since 9.4
41+
*/
42+
public boolean isVolatile();
43+
44+
/**
45+
* Sets the <code>volatile</code> qualifier.
46+
*
47+
* @since 9.4
48+
*/
49+
public void setVolatile(boolean value);
50+
51+
/**
52+
* Returns whether the <code>inline</code> qualifier was present.
53+
*
54+
* @since 9.4
55+
*/
56+
public boolean isInline();
57+
58+
/**
59+
* Sets the <code>inline</code> qualifier.
60+
*
61+
* @since 9.4
62+
*/
63+
public void setInline(boolean value);
64+
65+
/**
66+
* Returns whether the <code>goto</code> qualifier was present.
67+
*
68+
* @since 9.4
69+
*/
70+
public boolean isGoto();
71+
72+
/**
73+
* Sets the <code>goto</code> qualifier.
74+
*
75+
* @since 9.4
76+
*/
77+
public void setGoto(boolean value);
78+
3779
/**
3880
* @since 5.1
3981
*/

core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/AbstractGNUSourceCodeParser.java

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1867,8 +1867,26 @@ protected IASTDeclaration[] problemDeclaration(int offset, BacktrackException bt
18671867

18681868
protected IASTDeclaration asmDeclaration() throws EndOfFileException, BacktrackException {
18691869
final int offset = consume().getOffset(); // t_asm
1870-
if (LT(1) == IToken.t_volatile) {
1871-
consume();
1870+
boolean isVolatile = false;
1871+
boolean isInline = false;
1872+
boolean isGoto = false;
1873+
loop: while (true) {
1874+
switch (LT(1)) {
1875+
case IToken.t_volatile:
1876+
isVolatile = true;
1877+
consume();
1878+
break;
1879+
case IToken.t_inline:
1880+
isInline = true;
1881+
consume();
1882+
break;
1883+
case IToken.t_goto:
1884+
isGoto = true;
1885+
consume();
1886+
break;
1887+
default:
1888+
break loop;
1889+
}
18721890
}
18731891

18741892
if (supportFunctionStyleAsm && LT(1) != IToken.tLPAREN) {
@@ -1879,7 +1897,7 @@ protected IASTDeclaration asmDeclaration() throws EndOfFileException, BacktrackE
18791897
asmExpression(buffer);
18801898
int lastOffset = consume(IToken.tSEMI).getEndOffset();
18811899

1882-
return buildASMDirective(offset, buffer.toString(), lastOffset);
1900+
return buildASMDirective(offset, buffer.toString(), lastOffset, isVolatile, isInline, isGoto);
18831901
}
18841902

18851903
protected IASTDeclaration functionStyleAsmDeclaration() throws BacktrackException, EndOfFileException {
@@ -1956,7 +1974,15 @@ protected IToken asmExpression(StringBuilder content) throws EndOfFileException,
19561974
}
19571975

19581976
protected IASTASMDeclaration buildASMDirective(int offset, String assembly, int lastOffset) {
1977+
return buildASMDirective(offset, assembly, lastOffset, false, false, false);
1978+
}
1979+
1980+
protected IASTASMDeclaration buildASMDirective(int offset, String assembly, int lastOffset, boolean isVolatile,
1981+
boolean isInline, boolean isGoto) {
19591982
IASTASMDeclaration result = nodeFactory.newASMDeclaration(assembly);
1983+
result.setVolatile(isVolatile);
1984+
result.setInline(isInline);
1985+
result.setGoto(isGoto);
19601986
((ASTNode) result).setOffsetAndLength(offset, lastOffset - offset);
19611987
return result;
19621988
}

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
public class CASTASMDeclaration extends ASTNode implements IASTASMDeclaration {
2525

2626
char[] assembly = null;
27+
private boolean isVolatile;
28+
private boolean isInline;
29+
private boolean isGoto;
2730

2831
public CASTASMDeclaration() {
2932
}
@@ -41,6 +44,9 @@ public CASTASMDeclaration copy() {
4144
public CASTASMDeclaration copy(CopyStyle style) {
4245
CASTASMDeclaration copy = new CASTASMDeclaration();
4346
copy.assembly = assembly == null ? null : assembly.clone();
47+
copy.isVolatile = isVolatile;
48+
copy.isInline = isInline;
49+
copy.isGoto = isGoto;
4450
return copy(copy, style);
4551
}
4652

@@ -57,6 +63,39 @@ public void setAssembly(String assembly) {
5763
this.assembly = assembly == null ? null : assembly.toCharArray();
5864
}
5965

66+
@Override
67+
public boolean isVolatile() {
68+
return isVolatile;
69+
}
70+
71+
@Override
72+
public void setVolatile(boolean value) {
73+
assertNotFrozen();
74+
isVolatile = value;
75+
}
76+
77+
@Override
78+
public boolean isInline() {
79+
return isInline;
80+
}
81+
82+
@Override
83+
public void setInline(boolean value) {
84+
assertNotFrozen();
85+
isInline = value;
86+
}
87+
88+
@Override
89+
public boolean isGoto() {
90+
return isGoto;
91+
}
92+
93+
@Override
94+
public void setGoto(boolean value) {
95+
assertNotFrozen();
96+
isGoto = value;
97+
}
98+
6099
@Override
61100
public boolean accept(ASTVisitor action) {
62101
if (action.shouldVisitDeclarations) {

core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTASMDeclaration.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
*/
2323
public class CPPASTASMDeclaration extends ASTNode implements IASTASMDeclaration {
2424
char[] assembly = null;
25+
private boolean isVolatile;
26+
private boolean isInline;
27+
private boolean isGoto;
2528

2629
public CPPASTASMDeclaration() {
2730
}
@@ -39,6 +42,9 @@ public CPPASTASMDeclaration copy() {
3942
public CPPASTASMDeclaration copy(CopyStyle style) {
4043
CPPASTASMDeclaration copy = new CPPASTASMDeclaration();
4144
copy.assembly = assembly.clone();
45+
copy.isVolatile = isVolatile;
46+
copy.isInline = isInline;
47+
copy.isGoto = isGoto;
4248
return copy(copy, style);
4349
}
4450

@@ -55,6 +61,39 @@ public void setAssembly(String assembly) {
5561
this.assembly = assembly.toCharArray();
5662
}
5763

64+
@Override
65+
public boolean isVolatile() {
66+
return isVolatile;
67+
}
68+
69+
@Override
70+
public void setVolatile(boolean value) {
71+
assertNotFrozen();
72+
isVolatile = value;
73+
}
74+
75+
@Override
76+
public boolean isInline() {
77+
return isInline;
78+
}
79+
80+
@Override
81+
public void setInline(boolean value) {
82+
assertNotFrozen();
83+
isInline = value;
84+
}
85+
86+
@Override
87+
public boolean isGoto() {
88+
return isGoto;
89+
}
90+
91+
@Override
92+
public void setGoto(boolean value) {
93+
assertNotFrozen();
94+
isGoto = value;
95+
}
96+
5897
@Override
5998
public boolean accept(ASTVisitor action) {
6099
if (action.shouldVisitDeclarations) {

core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/DeclarationWriter.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public class DeclarationWriter extends NodeWriter {
6868
private static final char CLOSE_PAREN = ')';
6969
private static final char OPEN_BRACKET = '[';
7070
private static final char CLOSE_BRACKET = ']';
71-
private static final String ASM_START = "asm" + OPEN_PAREN; //$NON-NLS-1$
71+
private static final String ASM_START = "asm"; //$NON-NLS-1$
7272
private static final String TEMPLATE_DECLARATION = "template<"; //$NON-NLS-1$
7373
private static final String TEMPLATE_SPECIALIZATION = "template<> "; //$NON-NLS-1$
7474
private static final char COMMA = ',';
@@ -315,6 +315,16 @@ private void writeExplicitTemplateInstantiation(
315315

316316
private void writeASMDeclatation(IASTASMDeclaration asmDeclaration) {
317317
scribe.print(ASM_START);
318+
if (asmDeclaration.isVolatile()) {
319+
scribe.print(" volatile"); //$NON-NLS-1$
320+
}
321+
if (asmDeclaration.isInline()) {
322+
scribe.print(" inline"); //$NON-NLS-1$
323+
}
324+
if (asmDeclaration.isGoto()) {
325+
scribe.print(" goto"); //$NON-NLS-1$
326+
}
327+
scribe.print(OPEN_PAREN);
318328
scribe.print(asmDeclaration.getAssembly());
319329
scribe.print(CLOSE_PAREN);
320330
printSemicolon();

0 commit comments

Comments
 (0)