-
Notifications
You must be signed in to change notification settings - Fork 187
/
ASTWriterTester.java
138 lines (121 loc) · 5.42 KB
/
ASTWriterTester.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*******************************************************************************
* Copyright (c) 2008, 2013 Institute for Software, HSR Hochschule fuer Technik
* Rapperswil, University of applied sciences and others
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Institute for Software - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.core.parser.tests.rewrite.astwriter;
import java.util.Map;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.parser.ISourceCodeParser;
import org.eclipse.cdt.core.dom.parser.c.ANSICParserExtensionConfiguration;
import org.eclipse.cdt.core.dom.parser.c.GCCParserExtensionConfiguration;
import org.eclipse.cdt.core.dom.parser.c.ICParserExtensionConfiguration;
import org.eclipse.cdt.core.dom.parser.cpp.ANSICPPParserExtensionConfiguration;
import org.eclipse.cdt.core.dom.parser.cpp.GPPParserExtensionConfiguration;
import org.eclipse.cdt.core.dom.parser.cpp.ICPPParserExtensionConfiguration;
import org.eclipse.cdt.core.parser.FileContent;
import org.eclipse.cdt.core.parser.IParserLogService;
import org.eclipse.cdt.core.parser.IScanner;
import org.eclipse.cdt.core.parser.NullLogService;
import org.eclipse.cdt.core.parser.ParserLanguage;
import org.eclipse.cdt.core.parser.ParserMode;
import org.eclipse.cdt.core.parser.ScannerInfo;
import org.eclipse.cdt.core.parser.tests.ast2.AST2TestBase;
import org.eclipse.cdt.core.parser.tests.ast2.AST2TestBase.ScannerKind;
import org.eclipse.cdt.core.parser.tests.rewrite.RewriteBaseTest;
import org.eclipse.cdt.core.parser.tests.rewrite.TestHelper;
import org.eclipse.cdt.core.parser.tests.rewrite.TestSourceFile;
import org.eclipse.cdt.internal.core.dom.parser.c.GNUCSourceParser;
import org.eclipse.cdt.internal.core.dom.parser.cpp.GNUCPPSourceParser;
import org.eclipse.cdt.internal.core.dom.rewrite.ASTModificationMap;
import org.eclipse.cdt.internal.core.dom.rewrite.astwriter.ASTWriter;
import org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.ASTCommenter;
import org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.NodeCommentMap;
import org.eclipse.core.resources.IFile;
/**
* @author Guido Zgraggen
*/
public abstract class ASTWriterTester extends RewriteBaseTest {
private static final IParserLogService NULL_LOG = new NullLogService();
private IFile file;
public ASTWriterTester(String name, ASTWriterTestSourceFile file) {
super(name);
fileMap.put(file.getName(), file);
}
@Override
protected void setUp() throws Exception {
super.setUp();
for (TestSourceFile testFile : fileMap.values()) {
if (testFile.getSource().length() > 0) {
file = importFile(testFile.getName(), testFile.getSource());
}
}
}
@Override
protected void runTest() throws Throwable {
file = project.getFile("ASTWritterTest.h"); //$NON-NLS-1$
compareFiles(fileMap);
}
@Override
protected void compareFiles(Map<String, TestSourceFile> testResourceFiles) throws Exception {
for (String fileName : testResourceFiles.keySet()) {
TestSourceFile testFile = testResourceFiles.get(fileName);
String code = generateSource(testFile);
assertEquals(TestHelper.unifyNewLines(testFile.getExpectedSource()),
TestHelper.unifyNewLines(code + System.getProperty("line.separator"))); //$NON-NLS-1$
}
}
public String generateSource(TestSourceFile testFile) throws Exception {
IASTTranslationUnit unit = getParser(testFile).parse();
NodeCommentMap commentMap = ASTCommenter.getCommentedNodeMap(unit);
ASTModificationMap map = new ASTModificationMap();
map.getModificationsForNode(unit.getDeclarations()[0]);
ASTWriter writer = new ASTWriter();
return writer.write(unit, commentMap);
}
protected ISourceCodeParser getParser(TestSourceFile testFile) throws Exception {
FileContent codeReader = FileContent.create(file);
ParserLanguage language = getLanguage(testFile);
ScannerKind scannerKind = getScannerKind(testFile);
ScannerInfo scannerInfo = AST2TestBase.createScannerInfo(scannerKind);
IScanner scanner = AST2TestBase.createScanner(codeReader, language, ParserMode.COMPLETE_PARSE, scannerInfo);
ISourceCodeParser parser = null;
if (language == ParserLanguage.CPP) {
ICPPParserExtensionConfiguration config = null;
if (scannerKind.isUseGNUExtensions()) {
config = new GPPParserExtensionConfiguration();
} else {
config = new ANSICPPParserExtensionConfiguration();
}
parser = new GNUCPPSourceParser(scanner, ParserMode.COMPLETE_PARSE, NULL_LOG, config);
} else {
ICParserExtensionConfiguration config = null;
if (scannerKind.isUseGNUExtensions()) {
config = new GCCParserExtensionConfiguration();
} else {
config = new ANSICParserExtensionConfiguration();
}
parser = new GNUCSourceParser(scanner, ParserMode.COMPLETE_PARSE, NULL_LOG, config);
}
return parser;
}
private ScannerKind getScannerKind(TestSourceFile file) {
if (file instanceof ASTWriterTestSourceFile)
return ((ASTWriterTestSourceFile) file).getScannerKind();
return ScannerKind.STD;
}
private ParserLanguage getLanguage(TestSourceFile file) {
if (file instanceof ASTWriterTestSourceFile)
return ((ASTWriterTestSourceFile) file).getParserLanguage();
return ParserLanguage.CPP;
}
}