From a1e03d937a6e3d0492ded81f072254e8edcdebc2 Mon Sep 17 00:00:00 2001 From: Anton Leherbauer Date: Wed, 21 Jan 2009 14:56:53 +0000 Subject: [PATCH] Bug 206271 - [formatter] confused by gcc __attribute__ defs Bug 241819 - [formatter] Greater-than (>) in macros not formatted properly --- .../formatter/CodeFormatterVisitor.java | 88 +++++++++++-------- .../cdt/internal/formatter/Scribe.java | 17 ++-- .../cdt/ui/tests/text/CodeFormatterTest.java | 39 ++++++++ 3 files changed, 94 insertions(+), 50 deletions(-) diff --git a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CodeFormatterVisitor.java b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CodeFormatterVisitor.java index e5640793654..883bdb5b261 100644 --- a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CodeFormatterVisitor.java +++ b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CodeFormatterVisitor.java @@ -436,6 +436,7 @@ public class CodeFormatterVisitor extends CPPASTVisitor { // declarator final IASTDeclarator declarator= node.getDeclarator(); if (declarator != null) { + skipNonWhitespaceToNode(declarator); boolean needSpace= declarator.getPointerOperators().length > 0 && scribe.printComment(); if (needSpace) { scribe.space(); @@ -2459,43 +2460,48 @@ public class CodeFormatterVisitor extends CPPASTVisitor { 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 { - scribe.printTrailingComment(); - scribe.startNewLine(); - scribe.indent(); - thenStatement.accept(this); - if (elseStatement != null) { - scribe.startNewLine(); + if (node.getFileLocation().getNodeOffset() == thenStatement.getFileLocation().getNodeOffset()) { + startNode(thenStatement); + } + 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 { + 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) { - formatOpeningBrace(block_brace_position, insertSpaceBeforeOpeningBrace); - boolean endsWithMacroExpansion= endsWithMacroExpansion(block); + final boolean startsWithMacroExpansion= startsWithMacroExpansion(block); + if (!startsWithMacroExpansion) { + formatOpeningBrace(block_brace_position, insertSpaceBeforeOpeningBrace); + } else { + scribe.startNewLine(); + scribe.printComment(); + } + final boolean endsWithMacroExpansion= endsWithMacroExpansion(block); IASTStatement[] statements = block.getStatements(); final int statementsLength = statements.length; if (statementsLength != 0) { @@ -3085,7 +3097,7 @@ public class CodeFormatterVisitor extends CPPASTVisitor { } if (!endsWithMacroExpansion) { formatClosingBrace(block_brace_position); - } else { + } else if (!startsWithMacroExpansion) { if (DefaultCodeFormatterConstants.NEXT_LINE_SHIFTED.equals(block_brace_position)) { scribe.unIndent(); } diff --git a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/Scribe.java b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/Scribe.java index 7095fe90f29..82b59eb7d71 100644 --- a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/Scribe.java +++ b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/Scribe.java @@ -637,10 +637,8 @@ public class Scribe { try { scanner.resetTo(Math.max(startOffset, currentPosition), startOffset + length - 1); int parenLevel= 0; - boolean lastTokenWasGT= false; while (true) { boolean hasWhitespace= printComment(); - boolean isGT= false; currentToken= scanner.nextToken(); if (currentToken == null) { if (hasWhitespace) { @@ -677,8 +675,10 @@ public class Scribe { for (int i= 0; i < preferences.continuation_indentation; i++) { indent(); } - // HACK: avoid indent in same line - column= indentationLevel + 1; + if (column <= indentationLevel) { + // HACK: avoid indent in same line + column= indentationLevel + 1; + } } break; case Token.tRPAREN: @@ -690,12 +690,6 @@ public class Scribe { } print(currentToken.getLength(), hasWhitespace); break; - case Token.tGT: - if (lastTokenWasGT) { - print(currentToken.getLength(), true); - } - isGT= true; - break; case Token.tSEMI: print(currentToken.getLength(), preferences.insert_space_before_semicolon); break; @@ -723,7 +717,6 @@ public class Scribe { } } hasWhitespace= false; - lastTokenWasGT= isGT; } } finally { scannerEndPosition= savedScannerEndPos; @@ -1679,7 +1672,7 @@ public class Scribe { } boolean shouldSkip(int offset) { - return offset >= fSkipStartOffset && offset <= fSkipEndOffset; + return offset >= fSkipStartOffset; } void skipRange(int offset, int endOffset) { diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CodeFormatterTest.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CodeFormatterTest.java index e58a3b80f4d..7f9cad4ee4b 100644 --- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CodeFormatterTest.java +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CodeFormatterTest.java @@ -1091,4 +1091,43 @@ public class CodeFormatterTest extends BaseUITestCase { public void testDotStarAndArrowStarOperators_Bug257700() throws Exception { 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(); + } + }