mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-29 19:45:01 +02:00
Formatting of function-style macros inside namespaces.
This commit is contained in:
parent
e0111b55dc
commit
9a3e8b0073
2 changed files with 49 additions and 19 deletions
|
@ -358,6 +358,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
|
||||||
|
|
||||||
private MultiStatus fStatus;
|
private MultiStatus fStatus;
|
||||||
private int fOpenAngleBrackets;
|
private int fOpenAngleBrackets;
|
||||||
|
private IASTTranslationUnit ast;
|
||||||
|
|
||||||
public CodeFormatterVisitor(DefaultCodeFormatterOptions preferences, int offset, int length) {
|
public CodeFormatterVisitor(DefaultCodeFormatterOptions preferences, int offset, int length) {
|
||||||
localScanner = new Scanner() {
|
localScanner = new Scanner() {
|
||||||
|
@ -440,13 +441,20 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public int visit(IASTTranslationUnit tu) {
|
public int visit(IASTTranslationUnit tu) {
|
||||||
// fake new line
|
ast = tu;
|
||||||
|
// Fake new line
|
||||||
scribe.lastNumberOfNewLines = 1;
|
scribe.lastNumberOfNewLines = 1;
|
||||||
scribe.startNewLine();
|
scribe.startNewLine();
|
||||||
final int indentLevel= scribe.indentationLevel;
|
final int indentLevel= scribe.indentationLevel;
|
||||||
IASTPreprocessorMacroExpansion[] macroExpansions = tu.getMacroExpansions();
|
|
||||||
int m = 0;
|
|
||||||
IASTDeclaration[] decls= tu.getDeclarations();
|
IASTDeclaration[] decls= tu.getDeclarations();
|
||||||
|
formatDeclarations(decls, indentLevel);
|
||||||
|
scribe.printEndOfTranslationUnit();
|
||||||
|
return PROCESS_SKIP;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void formatDeclarations(IASTDeclaration[] decls, final int indentLevel) {
|
||||||
|
IASTPreprocessorMacroExpansion[] macroExpansions = ast.getMacroExpansions();
|
||||||
|
int m = 0;
|
||||||
for (int i = 0; i < decls.length; i++) {
|
for (int i = 0; i < decls.length; i++) {
|
||||||
IASTDeclaration declaration = decls[i];
|
IASTDeclaration declaration = decls[i];
|
||||||
if (!declaration.isPartOfTranslationUnitFile()) {
|
if (!declaration.isPartOfTranslationUnitFile()) {
|
||||||
|
@ -455,23 +463,42 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
|
||||||
try {
|
try {
|
||||||
int pos = scribe.scanner.getCurrentPosition();
|
int pos = scribe.scanner.getCurrentPosition();
|
||||||
IASTFileLocation declarationLocation = declaration.getFileLocation();
|
IASTFileLocation declarationLocation = declaration.getFileLocation();
|
||||||
int declarartionOffset = declarationLocation.getNodeOffset();
|
int declarationOffset = declarationLocation.getNodeOffset();
|
||||||
int declarationEndOffset = declarartionOffset + declarationLocation.getNodeLength();
|
|
||||||
for (; m < macroExpansions.length; m++) {
|
for (; m < macroExpansions.length; m++) {
|
||||||
IASTPreprocessorMacroExpansion macroExpansion = macroExpansions[m];
|
IASTPreprocessorMacroExpansion macroExpansion = macroExpansions[m];
|
||||||
IASTFileLocation macroLocation = macroExpansion.getFileLocation();
|
IASTFileLocation macroLocation = macroExpansion.getFileLocation();
|
||||||
int macroOffset = macroLocation.getNodeOffset();
|
int macroOffset = macroLocation.getNodeOffset();
|
||||||
if (macroOffset > declarartionOffset) {
|
if (macroOffset > declarationOffset) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
int macroEndOffset = macroOffset + macroLocation.getNodeLength();
|
int macroEndOffset = macroOffset + macroLocation.getNodeLength();
|
||||||
if (isFunctionStyleMacroExpansion(macroExpansion) && macroOffset >= pos &&
|
if (isFunctionStyleMacroExpansion(macroExpansion) && macroOffset >= pos) {
|
||||||
(macroEndOffset <= declarartionOffset || macroEndOffset >= declarationEndOffset)) {
|
// Find the last declaration overlapping with the macro.
|
||||||
// The function-style macro expansion either doesn't overlap with
|
for (int j = i + 1; j < decls.length; j++) {
|
||||||
// the following declaration, or the declaration is completely covered by
|
IASTDeclaration next = decls[j];
|
||||||
// the macro expansion. In both cases formatting is driven by the text of
|
if (!next.isPartOfTranslationUnitFile()) {
|
||||||
// parameters of the macro, not by the expanded code.
|
continue;
|
||||||
formatFunctionStyleMacroExpansion(macroExpansion);
|
}
|
||||||
|
IASTFileLocation nextLocation = next.getFileLocation();
|
||||||
|
int nextOffset = nextLocation.getNodeOffset();
|
||||||
|
if (macroEndOffset <= nextOffset) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
i = j;
|
||||||
|
declaration = next;
|
||||||
|
declarationLocation = nextLocation;
|
||||||
|
declarationOffset = declarationLocation.getNodeOffset();
|
||||||
|
}
|
||||||
|
int declarationEndOffset = declarationOffset + declarationLocation.getNodeLength();
|
||||||
|
if (macroEndOffset <= declarationOffset || macroEndOffset >= declarationEndOffset ||
|
||||||
|
macroEndOffset == declarationEndOffset - 1 && scribe.scanner.source[macroEndOffset] == ';') {
|
||||||
|
// The function-style macro expansion either doesn't overlap with
|
||||||
|
// the following declaration, or completely covers, with a possible
|
||||||
|
// exception for the trailing semicolon, one or more declarations.
|
||||||
|
// In both cases formatting is driven by the text of parameters of
|
||||||
|
// the macro, not by the expanded code.
|
||||||
|
formatFunctionStyleMacroExpansion(macroExpansion);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -492,8 +519,6 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
scribe.printEndOfTranslationUnit();
|
|
||||||
return PROCESS_SKIP;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isFunctionStyleMacroExpansion(IASTPreprocessorMacroExpansion macroExpansion) {
|
private boolean isFunctionStyleMacroExpansion(IASTPreprocessorMacroExpansion macroExpansion) {
|
||||||
|
@ -1059,10 +1084,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
|
||||||
scribe.indent();
|
scribe.indent();
|
||||||
}
|
}
|
||||||
scribe.startNewLine();
|
scribe.startNewLine();
|
||||||
for (IASTDeclaration declaration : memberDecls) {
|
formatDeclarations(memberDecls, scribe.indentationLevel);
|
||||||
declaration.accept(this);
|
|
||||||
scribe.startNewLine();
|
|
||||||
}
|
|
||||||
if (preferences.indent_body_declarations_compare_to_namespace_header) {
|
if (preferences.indent_body_declarations_compare_to_namespace_header) {
|
||||||
scribe.unIndent();
|
scribe.unIndent();
|
||||||
}
|
}
|
||||||
|
@ -1328,6 +1350,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
|
||||||
}
|
}
|
||||||
|
|
||||||
if (tailFormatter != null) {
|
if (tailFormatter != null) {
|
||||||
|
scribe.runTailFormatter();
|
||||||
scribe.setTailFormatter(null);
|
scribe.setTailFormatter(null);
|
||||||
}
|
}
|
||||||
// Body
|
// Body
|
||||||
|
|
|
@ -1319,11 +1319,18 @@ public class CodeFormatterTest extends BaseUITestCase {
|
||||||
//#define MY_MACRO(a, b, c)
|
//#define MY_MACRO(a, b, c)
|
||||||
//
|
//
|
||||||
//MY_MACRO(abcdefghijklmnopqrstuvwxyz,25,"very very very very very very very very very very long text");
|
//MY_MACRO(abcdefghijklmnopqrstuvwxyz,25,"very very very very very very very very very very long text");
|
||||||
|
//namespace ns {
|
||||||
|
//MY_MACRO(abcdefghijklmnopqrstuvwxyz,25,"very very very very very very very very very very long text");
|
||||||
|
//}
|
||||||
|
|
||||||
//#define MY_MACRO(a, b, c)
|
//#define MY_MACRO(a, b, c)
|
||||||
//
|
//
|
||||||
//MY_MACRO(abcdefghijklmnopqrstuvwxyz, 25,
|
//MY_MACRO(abcdefghijklmnopqrstuvwxyz, 25,
|
||||||
// "very very very very very very very very very very long text");
|
// "very very very very very very very very very very long text");
|
||||||
|
//namespace ns {
|
||||||
|
//MY_MACRO(abcdefghijklmnopqrstuvwxyz, 25,
|
||||||
|
// "very very very very very very very very very very long text");
|
||||||
|
//}
|
||||||
public void testFunctionStyleMacro_1() throws Exception {
|
public void testFunctionStyleMacro_1() throws Exception {
|
||||||
fOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, CCorePlugin.SPACE);
|
fOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, CCorePlugin.SPACE);
|
||||||
assertFormatterResult();
|
assertFormatterResult();
|
||||||
|
|
Loading…
Add table
Reference in a new issue