1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

Fixed 90851 - [DOM AST] NPE in LocationMap.defineObjectStyleMacro

This commit is contained in:
John Camelon 2005-04-10 21:54:27 +00:00
parent c35557f172
commit 26907961a3
3 changed files with 279 additions and 250 deletions

View file

@ -11,6 +11,7 @@
package org.eclipse.cdt.core.parser.tests.ast2; package org.eclipse.cdt.core.parser.tests.ast2;
import java.io.InputStream; import java.io.InputStream;
import java.util.Collections;
import junit.framework.Test; import junit.framework.Test;
import junit.framework.TestSuite; import junit.framework.TestSuite;
@ -21,16 +22,14 @@ import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTFileLocation; import org.eclipse.cdt.core.dom.ast.IASTFileLocation;
import org.eclipse.cdt.core.dom.ast.IASTNode; import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTNodeLocation; import org.eclipse.cdt.core.dom.ast.IASTNodeLocation;
import org.eclipse.cdt.core.dom.ast.IASTPreprocessorEndifStatement;
import org.eclipse.cdt.core.dom.ast.IASTPreprocessorIfdefStatement;
import org.eclipse.cdt.core.dom.ast.IASTPreprocessorIncludeStatement; import org.eclipse.cdt.core.dom.ast.IASTPreprocessorIncludeStatement;
import org.eclipse.cdt.core.dom.ast.IASTPreprocessorMacroDefinition; import org.eclipse.cdt.core.dom.ast.IASTPreprocessorMacroDefinition;
import org.eclipse.cdt.core.dom.ast.IASTPreprocessorStatement;
import org.eclipse.cdt.core.dom.ast.IASTProblem; import org.eclipse.cdt.core.dom.ast.IASTProblem;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration; import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit; import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit.IDependencyTree;
import org.eclipse.cdt.core.parser.CodeReader; import org.eclipse.cdt.core.parser.CodeReader;
import org.eclipse.cdt.core.parser.ExtendedScannerInfo;
import org.eclipse.cdt.core.parser.IExtendedScannerInfo;
import org.eclipse.cdt.core.parser.IParserLogService; import org.eclipse.cdt.core.parser.IParserLogService;
import org.eclipse.cdt.core.parser.IScanner; import org.eclipse.cdt.core.parser.IScanner;
import org.eclipse.cdt.core.parser.IScannerInfo; import org.eclipse.cdt.core.parser.IScannerInfo;
@ -58,264 +57,282 @@ import org.eclipse.core.resources.IFile;
*/ */
public class DOMLocationInclusionTests extends FileBasePluginTest { public class DOMLocationInclusionTests extends FileBasePluginTest {
private static final IScannerInfo SCANNER_INFO = new ScannerInfo(); private static final IScannerInfo SCANNER_INFO = new ScannerInfo();
private static final IParserLogService NULL_LOG = new NullLogService(); private static final IParserLogService NULL_LOG = new NullLogService();
private static final ICodeReaderFactory factory = CDOM private static final ICodeReaderFactory factory = CDOM.getInstance()
.getInstance() .getCodeReaderFactory(CDOM.PARSE_SAVED_RESOURCES);
.getCodeReaderFactory(
CDOM.PARSE_SAVED_RESOURCES);
/** private static final String[] EMPTY_STRING_ARRAY = new String[0];
* @param name
* @param className
*/
public DOMLocationInclusionTests(String name) {
super(name, DOMLocationInclusionTests.class);
}
protected IASTTranslationUnit parse(IFile code, ParserLanguage language) /**
throws Exception { * @param name
InputStream stream = code.getContents(); * @param className
IScanner scanner = new DOMScanner(new CodeReader(code.getLocation() */
.toOSString(), stream), SCANNER_INFO, ParserMode.COMPLETE_PARSE, public DOMLocationInclusionTests(String name) {
language, NULL_LOG, getScannerConfig(language), factory); super(name, DOMLocationInclusionTests.class);
ISourceCodeParser parser = null; }
if (language == ParserLanguage.CPP) {
parser = new GNUCPPSourceParser(scanner, ParserMode.COMPLETE_PARSE,
NULL_LOG, new GPPParserExtensionConfiguration());
} else {
parser = new GNUCSourceParser(scanner, ParserMode.COMPLETE_PARSE,
NULL_LOG, new GCCParserExtensionConfiguration());
}
stream.close();
IASTTranslationUnit parseResult = parser.parse();
if (parser.encounteredError()) protected IASTTranslationUnit parse(IFile code, ParserLanguage language)
throw new ParserException("FAILURE"); //$NON-NLS-1$ throws Exception {
return parse(code, language, SCANNER_INFO);
}
if (language == ParserLanguage.C) { protected IASTTranslationUnit parse(IFile code, ParserLanguage language,
IASTProblem[] problems = CVisitor.getProblems(parseResult); IScannerInfo s) throws Exception {
assertEquals(problems.length, 0); InputStream stream = code.getContents();
} else if (language == ParserLanguage.CPP) { IScanner scanner = new DOMScanner(new CodeReader(code.getLocation()
IASTProblem[] problems = CPPVisitor.getProblems(parseResult); .toOSString(), stream), s, ParserMode.COMPLETE_PARSE, language,
assertEquals(problems.length, 0); NULL_LOG, getScannerConfig(language), factory);
} ISourceCodeParser parser = null;
if (language == ParserLanguage.CPP) {
parser = new GNUCPPSourceParser(scanner, ParserMode.COMPLETE_PARSE,
NULL_LOG, new GPPParserExtensionConfiguration());
} else {
parser = new GNUCSourceParser(scanner, ParserMode.COMPLETE_PARSE,
NULL_LOG, new GCCParserExtensionConfiguration());
}
stream.close();
IASTTranslationUnit parseResult = parser.parse();
return parseResult; if (parser.encounteredError())
} throw new ParserException("FAILURE"); //$NON-NLS-1$
/** if (language == ParserLanguage.C) {
* @param language IASTProblem[] problems = CVisitor.getProblems(parseResult);
* @return assertEquals(problems.length, 0);
*/ } else if (language == ParserLanguage.CPP) {
private IScannerExtensionConfiguration getScannerConfig( IASTProblem[] problems = CPPVisitor.getProblems(parseResult);
ParserLanguage language) { assertEquals(problems.length, 0);
if (language == ParserLanguage.CPP) }
return new GPPScannerExtensionConfiguration();
return new GCCScannerExtensionConfiguration();
}
/** return parseResult;
* @param pathEndsWith }
* TODO
* @param offset
* @param length
* @param declarator
*/
private void assertSoleFileLocation(IASTNode n, String pathEndsWith,
int offset, int length) {
IASTNodeLocation[] locations = n.getNodeLocations();
assertEquals(locations.length, 1);
IASTFileLocation nodeLocation = (IASTFileLocation) locations[0];
assertTrue(nodeLocation.getFileName().endsWith(pathEndsWith));
assertEquals(offset, nodeLocation.getNodeOffset());
assertEquals(length, nodeLocation.getNodeLength());
}
public void testSimpleInclusion() throws Exception { /**
String foo = "int FOO;"; //$NON-NLS-1$ * @param language
String code = "int bar;\n#include \"foo.h\"\n"; //$NON-NLS-1$ * @return
*/
private IScannerExtensionConfiguration getScannerConfig(
ParserLanguage language) {
if (language == ParserLanguage.CPP)
return new GPPScannerExtensionConfiguration();
return new GCCScannerExtensionConfiguration();
}
importFile("foo.h", foo); //$NON-NLS-1$ /**
IFile cpp = importFile("code.cpp", code); //$NON-NLS-1$ * @param pathEndsWith
* TODO
* @param offset
* @param length
* @param declarator
*/
private void assertSoleFileLocation(IASTNode n, String pathEndsWith,
int offset, int length) {
IASTNodeLocation[] locations = n.getNodeLocations();
assertEquals(locations.length, 1);
IASTFileLocation nodeLocation = (IASTFileLocation) locations[0];
assertTrue(nodeLocation.getFileName().endsWith(pathEndsWith));
assertEquals(offset, nodeLocation.getNodeOffset());
assertEquals(length, nodeLocation.getNodeLength());
}
for (ParserLanguage p = ParserLanguage.C; p != null; p = (p == ParserLanguage.C) ? ParserLanguage.CPP public void testSimpleInclusion() throws Exception {
: null) { String foo = "int FOO;"; //$NON-NLS-1$
IASTTranslationUnit tu = parse(cpp, p); //$NON-NLS-1$ String code = "int bar;\n#include \"foo.h\"\n"; //$NON-NLS-1$
IASTDeclaration[] declarations = tu.getDeclarations();
assertEquals(declarations.length, 2);
IASTSimpleDeclaration bar = (IASTSimpleDeclaration) declarations[0];
IASTSimpleDeclaration FOO = (IASTSimpleDeclaration) declarations[1];
assertSoleFileLocation(bar,
"code.cpp", code.indexOf("int"), code.indexOf(";") + 1); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
assertSoleFileLocation(FOO, importFile("foo.h", foo); //$NON-NLS-1$
"foo.h", foo.indexOf("int"), foo.indexOf(";") + 1); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ IFile cpp = importFile("code.cpp", code); //$NON-NLS-1$
IASTPreprocessorIncludeStatement[] incs = tu.getIncludeDirectives();
assertNotNull(incs);
assertEquals(incs.length, 1);
assertSoleFileLocation(
incs[0],
"code.cpp", code.indexOf("#inc"), code.indexOf(".h\"\n") + ".h\"".length() - code.indexOf("#inc")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
} for (ParserLanguage p = ParserLanguage.C; p != null; p = (p == ParserLanguage.C) ? ParserLanguage.CPP
} : null) {
IASTTranslationUnit tu = parse(cpp, p); //$NON-NLS-1$
IASTDeclaration[] declarations = tu.getDeclarations();
assertEquals(declarations.length, 2);
IASTSimpleDeclaration bar = (IASTSimpleDeclaration) declarations[0];
IASTSimpleDeclaration FOO = (IASTSimpleDeclaration) declarations[1];
assertSoleFileLocation(bar,
"code.cpp", code.indexOf("int"), code.indexOf(";") + 1); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
public void testSimpleInclusion2() throws Exception { assertSoleFileLocation(FOO,
String foo = "int FOO;"; //$NON-NLS-1$ "foo.h", foo.indexOf("int"), foo.indexOf(";") + 1); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
String code = "int bar;\n#include \"foo.h\"\nfloat byob;\n"; //$NON-NLS-1$ IASTPreprocessorIncludeStatement[] incs = tu.getIncludeDirectives();
assertNotNull(incs);
assertEquals(incs.length, 1);
assertSoleFileLocation(
incs[0],
"code.cpp", code.indexOf("#inc"), code.indexOf(".h\"\n") + ".h\"".length() - code.indexOf("#inc")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
importFile("foo.h", foo); //$NON-NLS-1$ }
IFile cpp = importFile("code.cpp", code); //$NON-NLS-1$ }
for (ParserLanguage p = ParserLanguage.C; p != null; p = (p == ParserLanguage.C) ? ParserLanguage.CPP public void testSimpleInclusion2() throws Exception {
: null) { String foo = "int FOO;"; //$NON-NLS-1$
IASTTranslationUnit tu = parse(cpp, p); //$NON-NLS-1$ String code = "int bar;\n#include \"foo.h\"\nfloat byob;\n"; //$NON-NLS-1$
IASTDeclaration[] declarations = tu.getDeclarations();
assertEquals(declarations.length, 3);
IASTSimpleDeclaration bar = (IASTSimpleDeclaration) declarations[0];
IASTSimpleDeclaration FOO = (IASTSimpleDeclaration) declarations[1];
IASTSimpleDeclaration byob = (IASTSimpleDeclaration) declarations[2];
assertSoleFileLocation(
bar,
"code.cpp", code.indexOf("int"), code.indexOf("r;") + 2 - code.indexOf("int")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
assertSoleFileLocation(
FOO,
"foo.h", foo.indexOf("int"), foo.indexOf(";") + 1 - foo.indexOf("int")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
assertSoleFileLocation(
byob,
"code.cpp", code.indexOf("float"), code.indexOf("b;") + 2 - code.indexOf("float")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
IASTPreprocessorIncludeStatement[] incs = tu.getIncludeDirectives();
assertNotNull(incs);
assertEquals(incs.length, 1);
assertSoleFileLocation(
incs[0],
"code.cpp", code.indexOf("#inc"), code.indexOf(".h\"\n") + ".h\"".length() - code.indexOf("#inc")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
}
}
public void testMacrosInIncludeFile() throws Exception { importFile("foo.h", foo); //$NON-NLS-1$
String c_file_code = "#define X 4\n\n#include \"blarg.h\"\n\n#define POST_INCLUDE\n\n"; //$NON-NLS-1$ IFile cpp = importFile("code.cpp", code); //$NON-NLS-1$
String h_file_code = "#ifndef _BLARG_H_\r\n#define _BLARG_H_\r\n// macro\r\n#define PRINT(s,m) printf(s,m)\r\n#endif //_BLARG_H_\r\n"; //$NON-NLS-1$
importFile("blarg.h", h_file_code); //$NON-NLS-1$
IFile c_file = importFile("blarg.c", c_file_code); //$NON-NLS-1$
for (ParserLanguage p = ParserLanguage.C; p != null; p = (p == ParserLanguage.C) ? ParserLanguage.CPP
: null) {
IASTTranslationUnit tu = parse(c_file, p); //$NON-NLS-1$
assertEquals(tu.getDeclarations().length, 0);
IASTPreprocessorMacroDefinition[] macroDefinitions = tu
.getMacroDefinitions();
assertNotNull(macroDefinitions);
assertEquals(macroDefinitions.length, 4);
assertSoleFileLocation(
macroDefinitions[0],
"blarg.c", c_file_code.indexOf("#define"), c_file_code.indexOf("4") + 1 - c_file_code.indexOf("#define")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
assertSoleFileLocation(macroDefinitions[0].getName(),
"blarg.c", c_file_code.indexOf("X"), 1); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
assertSoleFileLocation(
macroDefinitions[1],
"blarg.h", h_file_code.indexOf("#define _BLARG_H_"), "#define _BLARG_H_\r".length()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
assertSoleFileLocation(
macroDefinitions[1].getName(),
"blarg.h", h_file_code.indexOf("e _BLARG_H_") + 2, "_BLARG_H_".length()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
assertSoleFileLocation(
macroDefinitions[2],
"blarg.h", h_file_code.indexOf("#define PRINT(s,m) printf(s,m)\r"), "#define PRINT(s,m) printf(s,m)".length()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
assertSoleFileLocation(macroDefinitions[2].getName(),
"blarg.h", h_file_code.indexOf("PRINT"), "PRINT".length()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
assertSoleFileLocation(
macroDefinitions[3],
"blarg.c", c_file_code.indexOf("#define POST_INCLUDE"), "#define POST_INCLUDE".length()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
assertSoleFileLocation(
macroDefinitions[3].getName(),
"blarg.c", c_file_code.indexOf("POST_INCLUDE"), "POST_INCLUDE".length()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
}
}
public void testBug84451() throws Exception { for (ParserLanguage p = ParserLanguage.C; p != null; p = (p == ParserLanguage.C) ? ParserLanguage.CPP
String header1_code = "int x;\n"; //$NON-NLS-1$ : null) {
String header2_code = "int y;\n"; //$NON-NLS-1$ IASTTranslationUnit tu = parse(cpp, p); //$NON-NLS-1$
String cpp_code = "#include \"header1.h\"\n#include \"header2.h\"\nint z;\n"; //$NON-NLS-1$ IASTDeclaration[] declarations = tu.getDeclarations();
importFile("header1.h", header1_code); //$NON-NLS-1$ assertEquals(declarations.length, 3);
importFile("header2.h", header2_code); //$NON-NLS-1$ IASTSimpleDeclaration bar = (IASTSimpleDeclaration) declarations[0];
IFile f = importFile("source.c", cpp_code); //$NON-NLS-1$ IASTSimpleDeclaration FOO = (IASTSimpleDeclaration) declarations[1];
for (ParserLanguage p = ParserLanguage.C; p != null; p = (p == ParserLanguage.C) ? ParserLanguage.CPP IASTSimpleDeclaration byob = (IASTSimpleDeclaration) declarations[2];
: null) { assertSoleFileLocation(
IASTTranslationUnit tu = parse(f, p); bar,
IASTDeclaration[] declarations = tu.getDeclarations(); "code.cpp", code.indexOf("int"), code.indexOf("r;") + 2 - code.indexOf("int")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
IASTPreprocessorIncludeStatement[] includeDirectives = tu assertSoleFileLocation(
.getIncludeDirectives(); FOO,
assertSoleFileLocation( "foo.h", foo.indexOf("int"), foo.indexOf(";") + 1 - foo.indexOf("int")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
includeDirectives[0], assertSoleFileLocation(
"source.c", cpp_code.indexOf("#include \"header1.h\""), "#include \"header1.h\"".length()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ byob,
assertSoleFileLocation(declarations[0], "code.cpp", code.indexOf("float"), code.indexOf("b;") + 2 - code.indexOf("float")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
"header1.h", 0, "int x;".length()); //$NON-NLS-1$ //$NON-NLS-2$ IASTPreprocessorIncludeStatement[] incs = tu.getIncludeDirectives();
assertSoleFileLocation(declarations[1], assertNotNull(incs);
"header2.h", 0, "int y;".length()); //$NON-NLS-1$ //$NON-NLS-2$ assertEquals(incs.length, 1);
assertSoleFileLocation( assertSoleFileLocation(
includeDirectives[1], incs[0],
"source.c", cpp_code.indexOf("#include \"header2.h\""), "#include \"header2.h\"".length()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ "code.cpp", code.indexOf("#inc"), code.indexOf(".h\"\n") + ".h\"".length() - code.indexOf("#inc")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
assertSoleFileLocation(declarations[2], }
"source.c", cpp_code.indexOf("int z;"), "int z;".length()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ }
IASTTranslationUnit.IDependencyTree tree = tu.getDependencyTree(); public void testMacrosInIncludeFile() throws Exception {
assertEquals( tree.getInclusions().length, 2 ); String c_file_code = "#define X 4\n\n#include \"blarg.h\"\n\n#define POST_INCLUDE\n\n"; //$NON-NLS-1$
String h_file_code = "#ifndef _BLARG_H_\r\n#define _BLARG_H_\r\n// macro\r\n#define PRINT(s,m) printf(s,m)\r\n#endif //_BLARG_H_\r\n"; //$NON-NLS-1$
importFile("blarg.h", h_file_code); //$NON-NLS-1$
IFile c_file = importFile("blarg.c", c_file_code); //$NON-NLS-1$
for (ParserLanguage p = ParserLanguage.C; p != null; p = (p == ParserLanguage.C) ? ParserLanguage.CPP
: null) {
IASTTranslationUnit tu = parse(c_file, p); //$NON-NLS-1$
assertEquals(tu.getDeclarations().length, 0);
IASTPreprocessorMacroDefinition[] macroDefinitions = tu
.getMacroDefinitions();
assertNotNull(macroDefinitions);
assertEquals(macroDefinitions.length, 4);
assertSoleFileLocation(
macroDefinitions[0],
"blarg.c", c_file_code.indexOf("#define"), c_file_code.indexOf("4") + 1 - c_file_code.indexOf("#define")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
assertSoleFileLocation(macroDefinitions[0].getName(),
"blarg.c", c_file_code.indexOf("X"), 1); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
assertSoleFileLocation(
macroDefinitions[1],
"blarg.h", h_file_code.indexOf("#define _BLARG_H_"), "#define _BLARG_H_\r".length()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
assertSoleFileLocation(
macroDefinitions[1].getName(),
"blarg.h", h_file_code.indexOf("e _BLARG_H_") + 2, "_BLARG_H_".length()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
assertSoleFileLocation(
macroDefinitions[2],
"blarg.h", h_file_code.indexOf("#define PRINT(s,m) printf(s,m)\r"), "#define PRINT(s,m) printf(s,m)".length()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
assertSoleFileLocation(macroDefinitions[2].getName(),
"blarg.h", h_file_code.indexOf("PRINT"), "PRINT".length()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
assertSoleFileLocation(
macroDefinitions[3],
"blarg.c", c_file_code.indexOf("#define POST_INCLUDE"), "#define POST_INCLUDE".length()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
assertSoleFileLocation(
macroDefinitions[3].getName(),
"blarg.c", c_file_code.indexOf("POST_INCLUDE"), "POST_INCLUDE".length()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
}
}
} public void testBug84451() throws Exception {
} String header1_code = "int x;\n"; //$NON-NLS-1$
String header2_code = "int y;\n"; //$NON-NLS-1$
String cpp_code = "#include \"header1.h\"\n#include \"header2.h\"\nint z;\n"; //$NON-NLS-1$
importFile("header1.h", header1_code); //$NON-NLS-1$
importFile("header2.h", header2_code); //$NON-NLS-1$
IFile f = importFile("source.c", cpp_code); //$NON-NLS-1$
for (ParserLanguage p = ParserLanguage.C; p != null; p = (p == ParserLanguage.C) ? ParserLanguage.CPP
: null) {
IASTTranslationUnit tu = parse(f, p);
IASTDeclaration[] declarations = tu.getDeclarations();
IASTPreprocessorIncludeStatement[] includeDirectives = tu
.getIncludeDirectives();
assertSoleFileLocation(
includeDirectives[0],
"source.c", cpp_code.indexOf("#include \"header1.h\""), "#include \"header1.h\"".length()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
assertSoleFileLocation(declarations[0],
"header1.h", 0, "int x;".length()); //$NON-NLS-1$ //$NON-NLS-2$
assertSoleFileLocation(declarations[1],
"header2.h", 0, "int y;".length()); //$NON-NLS-1$ //$NON-NLS-2$
assertSoleFileLocation(
includeDirectives[1],
"source.c", cpp_code.indexOf("#include \"header2.h\""), "#include \"header2.h\"".length()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
assertSoleFileLocation(declarations[2],
"source.c", cpp_code.indexOf("int z;"), "int z;".length()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
public void testMacrosInIncludeFile2() throws Exception { IASTTranslationUnit.IDependencyTree tree = tu.getDependencyTree();
String c_file_code = "#define X 4\n\n#include \"blarg.h\"\n\n#define POST_INCLUDE\n#include \"second.h\"\n#define POST_SECOND\n"; //$NON-NLS-1$ assertEquals(tree.getInclusions().length, 2);
String h_file_code = "#ifndef _BLARG_H_\r\n#define _BLARG_H_\r\n//macro\r\n#define PRINT(s,m) printf(s,m)\r\n#endif //_BLARG_H_\r\n"; //$NON-NLS-1$
String h_file2_code = "#ifndef _SECOND_H_ \n#define _SECOND_H_\n#endif\n"; //$NON-NLS-1$
importFile("blarg.h", h_file_code); //$NON-NLS-1$
importFile("second.h", h_file2_code); //$NON-NLS-1$
IFile c_file = importFile("blarg.c", c_file_code); //$NON-NLS-1$
for (ParserLanguage p = ParserLanguage.C; p != null; p = (p == ParserLanguage.C) ? ParserLanguage.CPP
: null) {
IASTTranslationUnit tu = parse(c_file, p); //$NON-NLS-1$
assertEquals(tu.getDeclarations().length, 0);
IASTPreprocessorMacroDefinition[] macroDefinitions = tu
.getMacroDefinitions();
assertNotNull(macroDefinitions);
assertEquals(macroDefinitions.length, 6);
assertSoleFileLocation(
macroDefinitions[0],
"blarg.c", c_file_code.indexOf("#define"), c_file_code.indexOf("4") + 1 - c_file_code.indexOf("#define")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
assertSoleFileLocation(macroDefinitions[0].getName(),
"blarg.c", c_file_code.indexOf("X"), 1); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
assertSoleFileLocation(
macroDefinitions[1],
"blarg.h", h_file_code.indexOf("#define _BLARG_H_"), "#define _BLARG_H_\r".length()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
assertSoleFileLocation(
macroDefinitions[1].getName(),
"blarg.h", h_file_code.indexOf("e _BLARG_H_") + 2, "_BLARG_H_".length()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
assertSoleFileLocation(
macroDefinitions[2],
"blarg.h", h_file_code.indexOf("#define PRINT(s,m) printf(s,m)\r"), "#define PRINT(s,m) printf(s,m)".length()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
assertSoleFileLocation(macroDefinitions[2].getName(),
"blarg.h", h_file_code.indexOf("PRINT"), "PRINT".length()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
assertSoleFileLocation(
macroDefinitions[3],
"blarg.c", c_file_code.indexOf("#define POST_INCLUDE"), "#define POST_INCLUDE".length()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
assertSoleFileLocation(
macroDefinitions[3].getName(),
"blarg.c", c_file_code.indexOf("POST_INCLUDE"), "POST_INCLUDE".length()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
assertSoleFileLocation(
macroDefinitions[4],
"second.h", h_file2_code.indexOf("#define _SECOND_H_"), "#define _SECOND_H_".length()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
assertSoleFileLocation(
macroDefinitions[5],
"blarg.c", c_file_code.indexOf("#define POST_SECOND"), "#define POST_SECOND".length()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
} }
} }
public static Test suite() { public void testMacrosInIncludeFile2() throws Exception {
TestSuite suite = new TestSuite(DOMLocationInclusionTests.class); String c_file_code = "#define X 4\n\n#include \"blarg.h\"\n\n#define POST_INCLUDE\n#include \"second.h\"\n#define POST_SECOND\n"; //$NON-NLS-1$
suite.addTest(new DOMLocationInclusionTests("cleanupProject")); //$NON-NLS-1$ String h_file_code = "#ifndef _BLARG_H_\r\n#define _BLARG_H_\r\n//macro\r\n#define PRINT(s,m) printf(s,m)\r\n#endif //_BLARG_H_\r\n"; //$NON-NLS-1$
return suite; String h_file2_code = "#ifndef _SECOND_H_ \n#define _SECOND_H_\n#endif\n"; //$NON-NLS-1$
} importFile("blarg.h", h_file_code); //$NON-NLS-1$
importFile("second.h", h_file2_code); //$NON-NLS-1$
IFile c_file = importFile("blarg.c", c_file_code); //$NON-NLS-1$
for (ParserLanguage p = ParserLanguage.C; p != null; p = (p == ParserLanguage.C) ? ParserLanguage.CPP
: null) {
IASTTranslationUnit tu = parse(c_file, p); //$NON-NLS-1$
assertEquals(tu.getDeclarations().length, 0);
IASTPreprocessorMacroDefinition[] macroDefinitions = tu
.getMacroDefinitions();
assertNotNull(macroDefinitions);
assertEquals(macroDefinitions.length, 6);
assertSoleFileLocation(
macroDefinitions[0],
"blarg.c", c_file_code.indexOf("#define"), c_file_code.indexOf("4") + 1 - c_file_code.indexOf("#define")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
assertSoleFileLocation(macroDefinitions[0].getName(),
"blarg.c", c_file_code.indexOf("X"), 1); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
assertSoleFileLocation(
macroDefinitions[1],
"blarg.h", h_file_code.indexOf("#define _BLARG_H_"), "#define _BLARG_H_\r".length()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
assertSoleFileLocation(
macroDefinitions[1].getName(),
"blarg.h", h_file_code.indexOf("e _BLARG_H_") + 2, "_BLARG_H_".length()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
assertSoleFileLocation(
macroDefinitions[2],
"blarg.h", h_file_code.indexOf("#define PRINT(s,m) printf(s,m)\r"), "#define PRINT(s,m) printf(s,m)".length()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
assertSoleFileLocation(macroDefinitions[2].getName(),
"blarg.h", h_file_code.indexOf("PRINT"), "PRINT".length()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
assertSoleFileLocation(
macroDefinitions[3],
"blarg.c", c_file_code.indexOf("#define POST_INCLUDE"), "#define POST_INCLUDE".length()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
assertSoleFileLocation(
macroDefinitions[3].getName(),
"blarg.c", c_file_code.indexOf("POST_INCLUDE"), "POST_INCLUDE".length()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
assertSoleFileLocation(
macroDefinitions[4],
"second.h", h_file2_code.indexOf("#define _SECOND_H_"), "#define _SECOND_H_".length()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
assertSoleFileLocation(
macroDefinitions[5],
"blarg.c", c_file_code.indexOf("#define POST_SECOND"), "#define POST_SECOND".length()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
}
}
public void testBug90851() throws Exception {
IFile imacro_file = importFile( "macro.h", "#define BEAST 666\n"); //$NON-NLS-1$ //$NON-NLS-2$
IFile include_file = importFile( "include.h", "int value = BEAST;\n"); //$NON-NLS-1$ //$NON-NLS-2$
String [] macros = { imacro_file.getLocation().toOSString() };
String [] includes = { include_file.getLocation().toOSString() };
IExtendedScannerInfo scannerInfo = new ExtendedScannerInfo( Collections.EMPTY_MAP, EMPTY_STRING_ARRAY, macros, includes );
IFile code = importFile( "main.c", "int main() { return value; } "); //$NON-NLS-1$ //$NON-NLS-2$
for (ParserLanguage p = ParserLanguage.C; p != null; p = (p == ParserLanguage.C) ? ParserLanguage.CPP
: null) {
parse(code, p, scannerInfo ); //$NON-NLS-1$
}
}
public static Test suite() {
TestSuite suite = new TestSuite(DOMLocationInclusionTests.class);
suite.addTest(new DOMLocationInclusionTests("cleanupProject")); //$NON-NLS-1$
return suite;
}
} }

View file

@ -1617,7 +1617,8 @@ abstract class BaseScanner implements IScanner {
* *
*/ */
protected void errorHandle() { protected void errorHandle() {
++bufferPos[bufferStackPos]; if( bufferStackPos > 0 )
++bufferPos[bufferStackPos];
} }
/** /**

View file

@ -49,6 +49,12 @@ import org.eclipse.cdt.internal.core.dom.parser.ASTPreprocessorSelectionResult;
*/ */
public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { public class LocationMap implements ILocationResolver, IScannerPreprocessorLog {
public LocationMap() {
tu = new _TranslationUnit();
currentContext = tu;
}
public class MacroExpansionLocation implements IASTMacroExpansion { public class MacroExpansionLocation implements IASTMacroExpansion {
public MacroExpansionLocation( public MacroExpansionLocation(
@ -1087,7 +1093,7 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog {
} }
protected static class _CompositeFileContext extends _CompositeContext { protected static class _CompositeFileContext extends _CompositeContext {
public final CodeReader reader; public CodeReader reader;
public _CompositeFileContext(_CompositeContext parent, int startOffset, public _CompositeFileContext(_CompositeContext parent, int startOffset,
int endOffset, CodeReader reader) { int endOffset, CodeReader reader) {
@ -1095,6 +1101,12 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog {
this.reader = reader; this.reader = reader;
} }
public _CompositeFileContext(_CompositeContext parent, int startOffset,
int endOffset ) {
super(parent, startOffset, endOffset);
}
public int getLineNumber(int nodeOffset) { public int getLineNumber(int nodeOffset) {
if( nodeOffset >= reader.buffer.length ) if( nodeOffset >= reader.buffer.length )
return 1; return 1;
@ -1124,8 +1136,8 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog {
* @param startOffset * @param startOffset
* @param endOffset * @param endOffset
*/ */
public _TranslationUnit(CodeReader reader) { public _TranslationUnit() {
super(null, 0, 0, reader); super(null, 0, 0 );
} }
IMacroDefinition [] builtins = new IMacroDefinition[2]; IMacroDefinition [] builtins = new IMacroDefinition[2];
@ -1773,8 +1785,7 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog {
* @see org.eclipse.cdt.internal.core.parser.scanner2.IScannerPreprocessorLog#startTranslationUnit() * @see org.eclipse.cdt.internal.core.parser.scanner2.IScannerPreprocessorLog#startTranslationUnit()
*/ */
public void startTranslationUnit(CodeReader tu_reader) { public void startTranslationUnit(CodeReader tu_reader) {
tu = new _TranslationUnit(tu_reader); tu.reader = tu_reader;
currentContext = tu;
} }
/* /*