diff options
author | Anton Leherbauer | 2009-01-21 14:57:06 +0000 |
---|---|---|
committer | Anton Leherbauer | 2009-01-21 14:57:06 +0000 |
commit | 59171007e5138fc373c9b4a808bcde8a01d297c4 (patch) | |
tree | 6d8e3a267a5c6e63823842294fa4ce7b4ed414c7 /core | |
parent | 5108ac0c47fb14991ffaef33a48b1d336695e430 (diff) | |
download | org.eclipse.cdt-59171007e5138fc373c9b4a808bcde8a01d297c4.tar.gz org.eclipse.cdt-59171007e5138fc373c9b4a808bcde8a01d297c4.tar.xz org.eclipse.cdt-59171007e5138fc373c9b4a808bcde8a01d297c4.zip |
Bug 206271 - [formatter] confused by gcc __attribute__ defs
Bug 241819 - [formatter] Greater-than (>) in macros not formatted properly
Diffstat (limited to 'core')
3 files changed, 93 insertions, 49 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 472b165eb5a..2e0fccf93e8 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 @@ -435,6 +435,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(); @@ -2471,43 +2472,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) { + 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(); } } @@ -3072,8 +3078,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) { @@ -3097,7 +3109,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 6051a621616..8b4911ff752 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 @@ -1046,5 +1046,44 @@ 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(); + } + } |