mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-07-28 19:35:36 +02:00
Fix for 217435: Braces in macros confuse code formatter
This commit is contained in:
parent
7f2fe153bd
commit
2b53efe517
5 changed files with 594 additions and 441 deletions
File diff suppressed because it is too large
Load diff
|
@ -1,5 +1,5 @@
|
||||||
/*******************************************************************************
|
/*******************************************************************************
|
||||||
* Copyright (c) 2000, 2007 IBM Corporation and others.
|
* Copyright (c) 2000, 2008 IBM Corporation and others.
|
||||||
* All rights reserved. This program and the accompanying materials
|
* All rights reserved. This program and the accompanying materials
|
||||||
* are made available under the terms of the Eclipse Public License v1.0
|
* are made available under the terms of the Eclipse Public License v1.0
|
||||||
* which accompanies this distribution, and is available at
|
* which accompanies this distribution, and is available at
|
||||||
|
@ -92,10 +92,14 @@ public class Scribe {
|
||||||
|
|
||||||
private boolean preserveNewlines;
|
private boolean preserveNewlines;
|
||||||
|
|
||||||
private List fSkipPositions= Collections.EMPTY_LIST;
|
private List<Position> fSkipPositions= Collections.emptyList();
|
||||||
|
|
||||||
private boolean skipOverInactive;
|
private boolean skipOverInactive;
|
||||||
|
|
||||||
|
private int fSkipTokensFrom= Integer.MAX_VALUE;
|
||||||
|
private int fSkipTokensTo;
|
||||||
|
private int fSkippedIndentations;
|
||||||
|
|
||||||
Scribe(CodeFormatterVisitor formatter, int offset, int length) {
|
Scribe(CodeFormatterVisitor formatter, int offset, int length) {
|
||||||
scanner= new Scanner();
|
scanner= new Scanner();
|
||||||
this.formatter= formatter;
|
this.formatter= formatter;
|
||||||
|
@ -532,6 +536,10 @@ public class Scribe {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void indent() {
|
public void indent() {
|
||||||
|
if (shouldSkip(scanner.getCurrentPosition())) {
|
||||||
|
fSkippedIndentations++;
|
||||||
|
return;
|
||||||
|
}
|
||||||
indentationLevel+= indentationSize;
|
indentationLevel+= indentationSize;
|
||||||
numberOfIndentations++;
|
numberOfIndentations++;
|
||||||
}
|
}
|
||||||
|
@ -549,7 +557,7 @@ public class Scribe {
|
||||||
/**
|
/**
|
||||||
* @param list
|
* @param list
|
||||||
*/
|
*/
|
||||||
public void setSkipPositions(List list) {
|
public void setSkipPositions(List<Position> list) {
|
||||||
fSkipPositions= list;
|
fSkipPositions= list;
|
||||||
skipOverInactive= !list.isEmpty();
|
skipOverInactive= !list.isEmpty();
|
||||||
}
|
}
|
||||||
|
@ -611,6 +619,9 @@ public class Scribe {
|
||||||
if (length <= 0) {
|
if (length <= 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (shouldSkip(scanner.getCurrentPosition())) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (startOffset > scanner.getCurrentPosition()) {
|
if (startOffset > scanner.getCurrentPosition()) {
|
||||||
printComment();
|
printComment();
|
||||||
}
|
}
|
||||||
|
@ -649,20 +660,26 @@ public class Scribe {
|
||||||
needSpace= false;
|
needSpace= false;
|
||||||
}
|
}
|
||||||
switch (currentToken.type) {
|
switch (currentToken.type) {
|
||||||
case Token.tLBRACE:
|
case Token.tLBRACE: {
|
||||||
|
// int currentPosition= scanner.getCurrentPosition();
|
||||||
scanner.resetTo(scanner.getCurrentTokenStartPosition(), scannerEndPosition-1);
|
scanner.resetTo(scanner.getCurrentTokenStartPosition(), scannerEndPosition-1);
|
||||||
formatOpeningBrace(formatter.preferences.brace_position_for_block, formatter.preferences.insert_space_before_opening_brace_in_block);
|
formatOpeningBrace(formatter.preferences.brace_position_for_block, formatter.preferences.insert_space_before_opening_brace_in_block);
|
||||||
if (formatter.preferences.indent_statements_compare_to_block) {
|
if (formatter.preferences.indent_statements_compare_to_block) {
|
||||||
indent();
|
indent();
|
||||||
}
|
}
|
||||||
|
// scanner.resetTo(currentPosition, scannerEndPosition-1);
|
||||||
break;
|
break;
|
||||||
case Token.tRBRACE:
|
}
|
||||||
|
case Token.tRBRACE: {
|
||||||
|
// int currentPosition= scanner.getCurrentPosition();
|
||||||
scanner.resetTo(scanner.getCurrentTokenStartPosition(), scannerEndPosition-1);
|
scanner.resetTo(scanner.getCurrentTokenStartPosition(), scannerEndPosition-1);
|
||||||
if (formatter.preferences.indent_statements_compare_to_block) {
|
if (formatter.preferences.indent_statements_compare_to_block) {
|
||||||
unIndent();
|
unIndent();
|
||||||
}
|
}
|
||||||
formatClosingBrace(formatter.preferences.brace_position_for_block);
|
formatClosingBrace(formatter.preferences.brace_position_for_block);
|
||||||
|
// scanner.resetTo(currentPosition, scannerEndPosition-1);
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
case Token.tLPAREN:
|
case Token.tLPAREN:
|
||||||
++parenLevel;
|
++parenLevel;
|
||||||
print(currentToken.getLength(), hasWhitespace);
|
print(currentToken.getLength(), hasWhitespace);
|
||||||
|
@ -902,14 +919,16 @@ public class Scribe {
|
||||||
// if we have a space between two tokens we ensure it will be dumped in
|
// if we have a space between two tokens we ensure it will be dumped in
|
||||||
// the formatted string
|
// the formatted string
|
||||||
int currentTokenStartPosition= scanner.getCurrentPosition();
|
int currentTokenStartPosition= scanner.getCurrentPosition();
|
||||||
|
if (shouldSkip(currentTokenStartPosition)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
boolean hasComment= false;
|
boolean hasComment= false;
|
||||||
boolean hasLineComment= false;
|
boolean hasLineComment= false;
|
||||||
boolean hasWhitespace= false;
|
boolean hasWhitespace= false;
|
||||||
int count= 0;
|
int count= 0;
|
||||||
while ((currentToken= scanner.nextToken()) != null) {
|
while ((currentToken= scanner.nextToken()) != null) {
|
||||||
Position inactivePos= null;
|
|
||||||
if (skipOverInactive) {
|
if (skipOverInactive) {
|
||||||
inactivePos= getInactivePosAt(scanner.getCurrentTokenStartPosition());
|
Position inactivePos= getInactivePosAt(scanner.getCurrentTokenStartPosition());
|
||||||
if (inactivePos != null) {
|
if (inactivePos != null) {
|
||||||
int startOffset= Math.min(scanner.getCurrentTokenStartPosition(), inactivePos.getOffset());
|
int startOffset= Math.min(scanner.getCurrentTokenStartPosition(), inactivePos.getOffset());
|
||||||
int endOffset= Math.min(scannerEndPosition, inactivePos.getOffset() + inactivePos.getLength());
|
int endOffset= Math.min(scannerEndPosition, inactivePos.getOffset() + inactivePos.getLength());
|
||||||
|
@ -1037,8 +1056,8 @@ public class Scribe {
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
private Position getInactivePosAt(int offset) {
|
private Position getInactivePosAt(int offset) {
|
||||||
for (Iterator iter= fSkipPositions.iterator(); iter.hasNext();) {
|
for (Iterator<Position> iter= fSkipPositions.iterator(); iter.hasNext();) {
|
||||||
Position pos= (Position) iter.next();
|
Position pos= iter.next();
|
||||||
if (pos.includes(offset)) {
|
if (pos.includes(offset)) {
|
||||||
return pos;
|
return pos;
|
||||||
}
|
}
|
||||||
|
@ -1220,23 +1239,15 @@ public class Scribe {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public void printNewLine() {
|
public void printNewLine() {
|
||||||
if (lastNumberOfNewLines >= 1) {
|
printNewLine(scanner.getCurrentTokenEndPosition() + 1);
|
||||||
column= 1; // ensure that the scribe is at the beginning of a new
|
|
||||||
// line
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
addInsertEdit(scanner.getCurrentTokenEndPosition() + 1, lineSeparator);
|
|
||||||
line++;
|
|
||||||
lastNumberOfNewLines= 1;
|
|
||||||
column= 1;
|
|
||||||
needSpace= false;
|
|
||||||
pendingSpace= false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void printNewLine(int insertPosition) {
|
public void printNewLine(int insertPosition) {
|
||||||
|
if (shouldSkip(insertPosition - 1)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (lastNumberOfNewLines >= 1) {
|
if (lastNumberOfNewLines >= 1) {
|
||||||
column= 1; // ensure that the scribe is at the beginning of a new
|
column= 1; // ensure that the scribe is at the beginning of a new line
|
||||||
// line
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
addInsertEdit(insertPosition, lineSeparator);
|
addInsertEdit(insertPosition, lineSeparator);
|
||||||
|
@ -1253,6 +1264,10 @@ public class Scribe {
|
||||||
|
|
||||||
public void printNextToken(int expectedTokenType, boolean considerSpaceIfAny) {
|
public void printNextToken(int expectedTokenType, boolean considerSpaceIfAny) {
|
||||||
printComment();
|
printComment();
|
||||||
|
if (shouldSkip(scanner.getCurrentPosition())) {
|
||||||
|
// print(0, considerSpaceIfAny);
|
||||||
|
return;
|
||||||
|
}
|
||||||
currentToken= scanner.nextToken();
|
currentToken= scanner.nextToken();
|
||||||
if (currentToken == null || expectedTokenType != currentToken.type) {
|
if (currentToken == null || expectedTokenType != currentToken.type) {
|
||||||
throw new AbortFormatting(
|
throw new AbortFormatting(
|
||||||
|
@ -1267,6 +1282,10 @@ public class Scribe {
|
||||||
|
|
||||||
public void printNextToken(int[] expectedTokenTypes, boolean considerSpaceIfAny) {
|
public void printNextToken(int[] expectedTokenTypes, boolean considerSpaceIfAny) {
|
||||||
printComment();
|
printComment();
|
||||||
|
if (shouldSkip(scanner.getCurrentPosition())) {
|
||||||
|
// print(0, considerSpaceIfAny);
|
||||||
|
return;
|
||||||
|
}
|
||||||
currentToken= scanner.nextToken();
|
currentToken= scanner.nextToken();
|
||||||
if (Arrays.binarySearch(expectedTokenTypes, currentToken.type) < 0) {
|
if (Arrays.binarySearch(expectedTokenTypes, currentToken.type) < 0) {
|
||||||
StringBuffer expectations= new StringBuffer(5);
|
StringBuffer expectations= new StringBuffer(5);
|
||||||
|
@ -1302,6 +1321,9 @@ public class Scribe {
|
||||||
// if we have a space between two tokens we ensure it will be dumped in
|
// if we have a space between two tokens we ensure it will be dumped in
|
||||||
// the formatted string
|
// the formatted string
|
||||||
int currentTokenStartPosition= scanner.getCurrentPosition();
|
int currentTokenStartPosition= scanner.getCurrentPosition();
|
||||||
|
if (shouldSkip(currentTokenStartPosition)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
boolean hasWhitespaces= false;
|
boolean hasWhitespaces= false;
|
||||||
boolean hasComment= false;
|
boolean hasComment= false;
|
||||||
boolean hasLineComment= false;
|
boolean hasLineComment= false;
|
||||||
|
@ -1435,6 +1457,9 @@ public class Scribe {
|
||||||
public void space() {
|
public void space() {
|
||||||
if (!needSpace)
|
if (!needSpace)
|
||||||
return;
|
return;
|
||||||
|
if (shouldSkip(scanner.getCurrentPosition())) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
lastNumberOfNewLines= 0;
|
lastNumberOfNewLines= 0;
|
||||||
pendingSpace= true;
|
pendingSpace= true;
|
||||||
column++;
|
column++;
|
||||||
|
@ -1470,6 +1495,10 @@ public class Scribe {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void unIndent() {
|
public void unIndent() {
|
||||||
|
if (shouldSkip(scanner.getCurrentPosition())) {
|
||||||
|
fSkippedIndentations--;
|
||||||
|
return;
|
||||||
|
}
|
||||||
indentationLevel-= indentationSize;
|
indentationLevel-= indentationSize;
|
||||||
numberOfIndentations--;
|
numberOfIndentations--;
|
||||||
}
|
}
|
||||||
|
@ -1477,8 +1506,11 @@ public class Scribe {
|
||||||
/**
|
/**
|
||||||
*/
|
*/
|
||||||
public void printModifiers() {
|
public void printModifiers() {
|
||||||
boolean isFirstModifier= true;
|
|
||||||
int currentTokenStartPosition= scanner.getCurrentPosition();
|
int currentTokenStartPosition= scanner.getCurrentPosition();
|
||||||
|
if (shouldSkip(currentTokenStartPosition)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
boolean isFirstModifier= true;
|
||||||
boolean hasComment= false;
|
boolean hasComment= false;
|
||||||
while ((currentToken= scanner.nextToken()) != null) {
|
while ((currentToken= scanner.nextToken()) != null) {
|
||||||
switch (currentToken.type) {
|
switch (currentToken.type) {
|
||||||
|
@ -1580,6 +1612,9 @@ public class Scribe {
|
||||||
*/
|
*/
|
||||||
public boolean skipToToken(int expectedTokenType) {
|
public boolean skipToToken(int expectedTokenType) {
|
||||||
int skipStart= scanner.getCurrentPosition();
|
int skipStart= scanner.getCurrentPosition();
|
||||||
|
if (shouldSkip(skipStart)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
int braceLevel= 0;
|
int braceLevel= 0;
|
||||||
int parenLevel= 0;
|
int parenLevel= 0;
|
||||||
while ((currentToken= scanner.nextToken()) != null) {
|
while ((currentToken= scanner.nextToken()) != null) {
|
||||||
|
@ -1634,4 +1669,44 @@ public class Scribe {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param offset
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
boolean shouldSkip(int offset) {
|
||||||
|
return offset >= fSkipTokensFrom && offset <= fSkipTokensTo;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void skipRange(int offset, int endOffset) {
|
||||||
|
if (offset == fSkipTokensFrom) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
final int currentPosition= scanner.getCurrentPosition();
|
||||||
|
if (currentPosition >= offset && currentPosition < endOffset) {
|
||||||
|
printRaw(offset, endOffset - offset);
|
||||||
|
}
|
||||||
|
fSkipTokensFrom= offset;
|
||||||
|
fSkipTokensTo= endOffset;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void resetToOffset(int offset) {
|
||||||
|
if (fSkipTokensTo > 0) {
|
||||||
|
fSkipTokensFrom= Integer.MAX_VALUE;
|
||||||
|
fSkipTokensTo= 0;
|
||||||
|
while (fSkippedIndentations > 0) {
|
||||||
|
indent();
|
||||||
|
fSkippedIndentations--;
|
||||||
|
}
|
||||||
|
while (fSkippedIndentations < 0) {
|
||||||
|
unIndent();
|
||||||
|
fSkippedIndentations++;
|
||||||
|
}
|
||||||
|
if (offset > scanner.getCurrentPosition()) {
|
||||||
|
printRaw(scanner.getCurrentPosition(), offset - scanner.getCurrentPosition());
|
||||||
|
scanner.resetTo(offset, scannerEndPosition - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -53,3 +53,10 @@ void bug183220() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// declaration with array initializer
|
||||||
|
long dummy[]= { 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
|
||||||
|
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
|
||||||
|
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
|
||||||
|
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
|
||||||
|
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
|
||||||
|
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, };
|
||||||
|
|
|
@ -28,3 +28,8 @@ void bug183220()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// declaration with array initializer
|
||||||
|
long dummy[]= { 100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,
|
||||||
|
100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,
|
||||||
|
100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,
|
||||||
|
};
|
||||||
|
|
|
@ -58,7 +58,7 @@ public class CodeFormatterTest extends BaseUITestCase {
|
||||||
String before= contents[0].toString();
|
String before= contents[0].toString();
|
||||||
IDocument document= new Document(before);
|
IDocument document= new Document(before);
|
||||||
String expected= contents[1].toString();
|
String expected= contents[1].toString();
|
||||||
TextEdit edit= CodeFormatterUtil.format(CodeFormatter.K_COMPILATION_UNIT, before, 0, TextUtilities.getDefaultLineDelimiter(document), fOptions);
|
TextEdit edit= CodeFormatterUtil.format(CodeFormatter.K_TRANSLATION_UNIT, before, 0, TextUtilities.getDefaultLineDelimiter(document), fOptions);
|
||||||
assertNotNull(edit);
|
assertNotNull(edit);
|
||||||
edit.apply(document);
|
edit.apply(document);
|
||||||
assertEquals(expected, document.get());
|
assertEquals(expected, document.get());
|
||||||
|
@ -353,4 +353,43 @@ public class CodeFormatterTest extends BaseUITestCase {
|
||||||
assertFormatterResult();
|
assertFormatterResult();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//#define break_start(); { int foo;
|
||||||
|
//#define break_end(); foo = 0; }
|
||||||
|
//
|
||||||
|
//void break_indenter(int a, int b) {
|
||||||
|
// break_start(); // This semicolon moves to its own line.
|
||||||
|
// if(a > b) {
|
||||||
|
// indentation_remains();
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// if(b>a)
|
||||||
|
// indentation_vanishes();
|
||||||
|
//
|
||||||
|
// break_end();
|
||||||
|
//
|
||||||
|
// if(b == a)
|
||||||
|
// indentation_remains();
|
||||||
|
//}
|
||||||
|
|
||||||
|
//#define break_start(); { int foo;
|
||||||
|
//#define break_end(); foo = 0; }
|
||||||
|
//
|
||||||
|
//void break_indenter(int a, int b) {
|
||||||
|
// break_start()
|
||||||
|
// ; // This semicolon moves to its own line.
|
||||||
|
// if (a > b) {
|
||||||
|
// indentation_remains();
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// if (b>a)
|
||||||
|
// indentation_vanishes();
|
||||||
|
//
|
||||||
|
// break_end();
|
||||||
|
//
|
||||||
|
// if (b == a)
|
||||||
|
// indentation_remains();
|
||||||
|
//}
|
||||||
|
public void testBracesInMacros_Bug217435() throws Exception {
|
||||||
|
assertFormatterResult();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue