1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-22 14:12:10 +02:00

Bug 206271 - [formatter] confused by gcc __attribute__ defs

Bug 241819 - [formatter] Greater-than (>) in macros not formatted properly
This commit is contained in:
Anton Leherbauer 2009-01-21 14:56:53 +00:00
parent 55244050c6
commit a1e03d937a
3 changed files with 94 additions and 50 deletions

View file

@ -436,6 +436,7 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
// declarator // declarator
final IASTDeclarator declarator= node.getDeclarator(); final IASTDeclarator declarator= node.getDeclarator();
if (declarator != null) { if (declarator != null) {
skipNonWhitespaceToNode(declarator);
boolean needSpace= declarator.getPointerOperators().length > 0 && scribe.printComment(); boolean needSpace= declarator.getPointerOperators().length > 0 && scribe.printComment();
if (needSpace) { if (needSpace) {
scribe.space(); scribe.space();
@ -2459,43 +2460,48 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
scribe.startNewLine(); scribe.startNewLine();
} }
} }
} else if (elseStatement == null && preferences.keep_simple_if_on_one_line) {
Alignment compactIfAlignment = scribe.createAlignment(
"compactIf", //$NON-NLS-1$
preferences.alignment_for_compact_if,
Alignment.R_OUTERMOST,
1,
scribe.scanner.getCurrentPosition(),
1,
false);
scribe.enterAlignment(compactIfAlignment);
boolean ok = false;
do {
try {
scribe.alignFragment(compactIfAlignment, 0);
scribe.space();
thenStatement.accept(this);
ok = true;
} catch (AlignmentException e) {
scribe.redoAlignment(e);
}
} while (!ok);
scribe.exitAlignment(compactIfAlignment, true);
} else if (preferences.keep_then_statement_on_same_line) {
scribe.space();
thenStatement.accept(this);
if (elseStatement != null) {
scribe.startNewLine();
}
} else { } else {
scribe.printTrailingComment(); if (node.getFileLocation().getNodeOffset() == thenStatement.getFileLocation().getNodeOffset()) {
scribe.startNewLine(); startNode(thenStatement);
scribe.indent(); }
thenStatement.accept(this); if (elseStatement == null && preferences.keep_simple_if_on_one_line) {
if (elseStatement != null) { Alignment compactIfAlignment = scribe.createAlignment(
scribe.startNewLine(); "compactIf", //$NON-NLS-1$
preferences.alignment_for_compact_if,
Alignment.R_OUTERMOST,
1,
scribe.scanner.getCurrentPosition(),
1,
false);
scribe.enterAlignment(compactIfAlignment);
boolean ok = false;
do {
try {
scribe.alignFragment(compactIfAlignment, 0);
scribe.space();
thenStatement.accept(this);
ok = true;
} catch (AlignmentException e) {
scribe.redoAlignment(e);
}
} while (!ok);
scribe.exitAlignment(compactIfAlignment, true);
} else if (preferences.keep_then_statement_on_same_line) {
scribe.space();
thenStatement.accept(this);
if (elseStatement != null) {
scribe.startNewLine();
}
} else {
scribe.printTrailingComment();
scribe.startNewLine();
scribe.indent();
thenStatement.accept(this);
if (elseStatement != null) {
scribe.startNewLine();
}
scribe.unIndent();
} }
scribe.unIndent();
} }
} }
@ -3060,8 +3066,14 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
} }
private void formatBlock(IASTCompoundStatement block, String block_brace_position, boolean insertSpaceBeforeOpeningBrace, boolean indentStatements) { private void formatBlock(IASTCompoundStatement block, String block_brace_position, boolean insertSpaceBeforeOpeningBrace, boolean indentStatements) {
formatOpeningBrace(block_brace_position, insertSpaceBeforeOpeningBrace); final boolean startsWithMacroExpansion= startsWithMacroExpansion(block);
boolean endsWithMacroExpansion= endsWithMacroExpansion(block); if (!startsWithMacroExpansion) {
formatOpeningBrace(block_brace_position, insertSpaceBeforeOpeningBrace);
} else {
scribe.startNewLine();
scribe.printComment();
}
final boolean endsWithMacroExpansion= endsWithMacroExpansion(block);
IASTStatement[] statements = block.getStatements(); IASTStatement[] statements = block.getStatements();
final int statementsLength = statements.length; final int statementsLength = statements.length;
if (statementsLength != 0) { if (statementsLength != 0) {
@ -3085,7 +3097,7 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
} }
if (!endsWithMacroExpansion) { if (!endsWithMacroExpansion) {
formatClosingBrace(block_brace_position); formatClosingBrace(block_brace_position);
} else { } else if (!startsWithMacroExpansion) {
if (DefaultCodeFormatterConstants.NEXT_LINE_SHIFTED.equals(block_brace_position)) { if (DefaultCodeFormatterConstants.NEXT_LINE_SHIFTED.equals(block_brace_position)) {
scribe.unIndent(); scribe.unIndent();
} }

View file

@ -637,10 +637,8 @@ public class Scribe {
try { try {
scanner.resetTo(Math.max(startOffset, currentPosition), startOffset + length - 1); scanner.resetTo(Math.max(startOffset, currentPosition), startOffset + length - 1);
int parenLevel= 0; int parenLevel= 0;
boolean lastTokenWasGT= false;
while (true) { while (true) {
boolean hasWhitespace= printComment(); boolean hasWhitespace= printComment();
boolean isGT= false;
currentToken= scanner.nextToken(); currentToken= scanner.nextToken();
if (currentToken == null) { if (currentToken == null) {
if (hasWhitespace) { if (hasWhitespace) {
@ -677,8 +675,10 @@ public class Scribe {
for (int i= 0; i < preferences.continuation_indentation; i++) { for (int i= 0; i < preferences.continuation_indentation; i++) {
indent(); indent();
} }
// HACK: avoid indent in same line if (column <= indentationLevel) {
column= indentationLevel + 1; // HACK: avoid indent in same line
column= indentationLevel + 1;
}
} }
break; break;
case Token.tRPAREN: case Token.tRPAREN:
@ -690,12 +690,6 @@ public class Scribe {
} }
print(currentToken.getLength(), hasWhitespace); print(currentToken.getLength(), hasWhitespace);
break; break;
case Token.tGT:
if (lastTokenWasGT) {
print(currentToken.getLength(), true);
}
isGT= true;
break;
case Token.tSEMI: case Token.tSEMI:
print(currentToken.getLength(), preferences.insert_space_before_semicolon); print(currentToken.getLength(), preferences.insert_space_before_semicolon);
break; break;
@ -723,7 +717,6 @@ public class Scribe {
} }
} }
hasWhitespace= false; hasWhitespace= false;
lastTokenWasGT= isGT;
} }
} finally { } finally {
scannerEndPosition= savedScannerEndPos; scannerEndPosition= savedScannerEndPos;
@ -1679,7 +1672,7 @@ public class Scribe {
} }
boolean shouldSkip(int offset) { boolean shouldSkip(int offset) {
return offset >= fSkipStartOffset && offset <= fSkipEndOffset; return offset >= fSkipStartOffset;
} }
void skipRange(int offset, int endOffset) { void skipRange(int offset, int endOffset) {

View file

@ -1091,4 +1091,43 @@ public class CodeFormatterTest extends BaseUITestCase {
public void testDotStarAndArrowStarOperators_Bug257700() throws Exception { public void testDotStarAndArrowStarOperators_Bug257700() throws Exception {
assertFormatterResult(); assertFormatterResult();
} }
//void zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz1(unsigned char __attribute__((unused)) x, unsigned char __attribute__((unused)) y){;}
//void zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz1(
// unsigned char __attribute__((unused)) x,
// unsigned char __attribute__((unused)) y) {
// ;
//}
public void test__attribute__InParameterDecl_Bug206271() throws Exception {
assertFormatterResult();
}
//#define assert(e) if(!(e)) printf("Failed assertion")
//void test(){assert(1 > 0);}
//#define assert(e) if(!(e)) printf("Failed assertion")
//void test() {
// assert(1 > 0);
//}
public void testMacroFormatting1_Bug241819() throws Exception {
assertFormatterResult();
}
//#define PRINT if(printswitch) printf
//void test(){int i=0;PRINT("Watch the format");if(i>0){i=1;}}
//#define PRINT if(printswitch) printf
//void test() {
// int i = 0;
// PRINT("Watch the format");
// if (i > 0) {
// i = 1;
// }
//}
public void testMacroFormatting2_Bug241819() throws Exception {
assertFormatterResult();
}
} }