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 1f21cee1d9c..1bbf55ce811 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 @@ -1246,7 +1246,7 @@ public class CodeFormatterVisitor extends CPPASTVisitor { final List initializers = Arrays.asList(node.getInitializers()); if (initializers.isEmpty() && preferences.keep_empty_array_initializer_on_one_line) { scribe.printNextToken(Token.tLBRACE, preferences.insert_space_before_opening_brace_in_array_initializer); - scribe.printNextToken(Token.tLBRACE, preferences.insert_space_between_empty_braces_in_array_initializer); + scribe.printNextToken(Token.tRBRACE, preferences.insert_space_between_empty_braces_in_array_initializer); } else { final int line= scribe.line; final String brace_position= preferences.brace_position_for_array_initializer; @@ -1301,7 +1301,39 @@ public class CodeFormatterVisitor extends CPPASTVisitor { } private int visit(IASTLiteralExpression node) { - scribe.printNextToken(peekNextToken(), scribe.printComment()); + if (node.getKind() == IASTLiteralExpression.lk_string_literal) { + // handle concatentation of string literals + if (node.getNodeLocations().length > 1) { + // cannot handle embedded macros + skipNode(node); + } else { + int token; + boolean needSpace= false; + final int line= scribe.line; + boolean indented= false; + try { + while (true) { + scribe.printCommentPreservingNewLines(); + scribe.printNextToken(Token.tSTRING, needSpace); + token= peekNextToken(); + if (token != Token.tSTRING) { + break; + } + needSpace= true; + if (!indented && line != scribe.line) { + indented= true; + scribe.indent(); + } + } + } finally { + if (indented) { + scribe.unIndent(); + } + } + } + } else { + scribe.printNextToken(peekNextToken(), scribe.printComment()); + } return PROCESS_SKIP; } 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 962d64ac67d..ec3c34d5780 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 @@ -608,8 +608,7 @@ public class Scribe { } public void printRaw(int startOffset, int length) { - assert length >= 0; - if (length == 0) { + if (length <= 0) { return; } if (startOffset > scanner.getCurrentPosition()) { @@ -873,8 +872,9 @@ public class Scribe { public void printEndOfTranslationUnit() { int currentTokenStartPosition= scanner.getCurrentPosition(); - printRaw(currentTokenStartPosition, scannerEndPosition - currentTokenStartPosition + 1); - return; + if (currentTokenStartPosition <= scannerEndPosition) { + printRaw(currentTokenStartPosition, scannerEndPosition - currentTokenStartPosition + 1); + } } public boolean printComment() { @@ -1582,4 +1582,14 @@ public class Scribe { return false; } + public boolean printCommentPreservingNewLines() { + final boolean savedPreserveNL= this.preserveNewlines; + this.preserveNewlines= true; + try { + return printComment(); + } finally { + this.preserveNewlines= savedPreserveNL; + } + } + } diff --git a/core/org.eclipse.cdt.ui.tests/resources/formatter/bugs/After.cpp b/core/org.eclipse.cdt.ui.tests/resources/formatter/bugs/After.cpp index 2fb7ce8829a..59ae61da929 100644 --- a/core/org.eclipse.cdt.ui.tests/resources/formatter/bugs/After.cpp +++ b/core/org.eclipse.cdt.ui.tests/resources/formatter/bugs/After.cpp @@ -23,3 +23,7 @@ AClass::AClass(int x) throw(int) : // keep space between decl spec and declarator int main(int argc, char **argv) { } +// handling of string concat +char* s1= "this " "is " "one " "string."; +char* s2= "this " "is " + "one " "string."; diff --git a/core/org.eclipse.cdt.ui.tests/resources/formatter/bugs/Before.cpp b/core/org.eclipse.cdt.ui.tests/resources/formatter/bugs/Before.cpp index 1163f09a416..233986195ec 100644 --- a/core/org.eclipse.cdt.ui.tests/resources/formatter/bugs/Before.cpp +++ b/core/org.eclipse.cdt.ui.tests/resources/formatter/bugs/Before.cpp @@ -11,3 +11,7 @@ AClass::AClass(int x)throw(int):ABaseClass(x){for (int i=0;i < 12;i++) {}} int main(int argc, char **argv) { } +// handling of string concat +char* s1= "this " "is " "one ""string."; +char* s2= "this " "is " +"one " "string.";