1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-23 08:55:25 +02:00

Fix formatter issues with string literal concatenation

This commit is contained in:
Anton Leherbauer 2007-03-12 15:36:44 +00:00
parent 1bc6b14d20
commit cb14d91f76
4 changed files with 56 additions and 6 deletions

View file

@ -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;
}

View file

@ -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;
}
}
}

View file

@ -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.";

View file

@ -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.";