1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-06-07 17:56:01 +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,7 +2460,11 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
scribe.startNewLine(); scribe.startNewLine();
} }
} }
} else if (elseStatement == null && preferences.keep_simple_if_on_one_line) { } else {
if (node.getFileLocation().getNodeOffset() == thenStatement.getFileLocation().getNodeOffset()) {
startNode(thenStatement);
}
if (elseStatement == null && preferences.keep_simple_if_on_one_line) {
Alignment compactIfAlignment = scribe.createAlignment( Alignment compactIfAlignment = scribe.createAlignment(
"compactIf", //$NON-NLS-1$ "compactIf", //$NON-NLS-1$
preferences.alignment_for_compact_if, preferences.alignment_for_compact_if,
@ -2498,6 +2503,7 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
scribe.unIndent(); scribe.unIndent();
} }
} }
}
if (elseStatement != null) { if (elseStatement != null) {
if (thenStatementIsBlock) { if (thenStatementIsBlock) {
@ -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) {
final boolean startsWithMacroExpansion= startsWithMacroExpansion(block);
if (!startsWithMacroExpansion) {
formatOpeningBrace(block_brace_position, insertSpaceBeforeOpeningBrace); formatOpeningBrace(block_brace_position, insertSpaceBeforeOpeningBrace);
boolean endsWithMacroExpansion= endsWithMacroExpansion(block); } 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,9 +675,11 @@ public class Scribe {
for (int i= 0; i < preferences.continuation_indentation; i++) { for (int i= 0; i < preferences.continuation_indentation; i++) {
indent(); indent();
} }
if (column <= indentationLevel) {
// HACK: avoid indent in same line // HACK: avoid indent in same line
column= indentationLevel + 1; column= indentationLevel + 1;
} }
}
break; break;
case Token.tRPAREN: case Token.tRPAREN:
--parenLevel; --parenLevel;
@ -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();
}
} }