mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-29 19:45:01 +02:00
Fixed Bug 97967 - [Locations] Problems involving offsets in tu which has ExtendedScannerInfo
This commit is contained in:
parent
b6a787a64a
commit
6314ff0d79
3 changed files with 147 additions and 117 deletions
|
@ -20,6 +20,7 @@ import org.eclipse.cdt.core.dom.CDOM;
|
|||
import org.eclipse.cdt.core.dom.IParserConfiguration;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTFileLocation;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTNode;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTNodeLocation;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTPreprocessorIncludeStatement;
|
||||
|
@ -41,43 +42,38 @@ import org.eclipse.core.runtime.content.IContentType;
|
|||
*/
|
||||
public class DOMLocationInclusionTests extends AST2FileBasePluginTest {
|
||||
|
||||
public class ParserConfiguration implements IParserConfiguration {
|
||||
public void testBug97967() throws Exception {
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
buffer.append("#ifndef _INCLUDE_H_\n"); //$NON-NLS-1$
|
||||
buffer.append("#define _INCLUDE_H_\n"); //$NON-NLS-1$
|
||||
buffer.append("typedef void (*vfp)();\n"); //$NON-NLS-1$
|
||||
buffer.append("typedef int (*ifp)();\n"); //$NON-NLS-1$
|
||||
buffer.append("struct Include {\n"); //$NON-NLS-1$
|
||||
buffer.append("int i;\n"); //$NON-NLS-1$
|
||||
buffer.append("};\n"); //$NON-NLS-1$
|
||||
buffer.append("#endif /*_INCLUDE_H_*/\n"); //$NON-NLS-1$
|
||||
String[] macros = { importFile(
|
||||
"macro.h", "#define JEDEN 1\n#define DVA 2\n#define TRI 3\n").getLocation().toOSString() }; //$NON-NLS-1$ //$NON-NLS-2$
|
||||
String[] includes = { importFile("include.h", buffer.toString()).getLocation().toOSString() }; //$NON-NLS-1$
|
||||
IExtendedScannerInfo scannerInfo = new ExtendedScannerInfo(
|
||||
Collections.EMPTY_MAP, EMPTY_STRING_ARRAY, macros, includes);
|
||||
String code = "int main() { return BEAST * sizeof( Include ); } "; //$NON-NLS-1$
|
||||
for (ParserLanguage p = ParserLanguage.C; p != null; p = (p == ParserLanguage.C) ? ParserLanguage.CPP
|
||||
: null) {
|
||||
String filename = (p == ParserLanguage.CPP) ? "main.cc" : "main.c"; //$NON-NLS-1$ //$NON-NLS-2$
|
||||
IFile c = importFile(filename, code); //$NON-NLS-1$ //$NON-NLS-2$
|
||||
|
||||
private final IScannerInfo info;
|
||||
private final String dialect;
|
||||
|
||||
public ParserConfiguration(IScannerInfo s, IFile code) {
|
||||
this.info = s;
|
||||
String filename = code.getLocation().toOSString();
|
||||
IProject prj = code.getProject();
|
||||
|
||||
//FIXME: ALAIN, for headers should we assume CPP ??
|
||||
// The problem is that it really depends on how the header was included.
|
||||
String id = null;
|
||||
IContentType contentType = CCorePlugin.getContentType(prj, filename);
|
||||
if (contentType != null) {
|
||||
id = contentType.getId();
|
||||
}
|
||||
|
||||
if (id != null) {
|
||||
if (CCorePlugin.CONTENT_TYPE_CSOURCE.equals(id))
|
||||
dialect = "GNUC"; //$NON-NLS-1$
|
||||
else
|
||||
dialect = "GNUC++"; //$NON-NLS-1$
|
||||
}
|
||||
else
|
||||
dialect = "GNUC++"; //$NON-NLS-1$
|
||||
|
||||
IASTTranslationUnit tu = parse(c, scannerInfo); //$NON-NLS-1$
|
||||
IASTFunctionDefinition fd = (IASTFunctionDefinition) tu
|
||||
.getDeclarations()[3];
|
||||
IASTFileLocation floc = fd.getFileLocation();
|
||||
assertEquals(
|
||||
floc.getNodeOffset(),
|
||||
code
|
||||
.indexOf("int main() { return BEAST * sizeof( Include ); }")); //$NON-NLS-1$
|
||||
assertEquals(floc.getNodeLength(),
|
||||
"int main() { return BEAST * sizeof( Include ); }".length()); //$NON-NLS-1$
|
||||
}
|
||||
|
||||
public IScannerInfo getScannerInfo() {
|
||||
return info;
|
||||
}
|
||||
|
||||
public String getParserDialect() {
|
||||
return dialect;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void testBug101875() throws Exception {
|
||||
|
@ -97,21 +93,62 @@ public class DOMLocationInclusionTests extends AST2FileBasePluginTest {
|
|||
buffer.append("*/\n"); //$NON-NLS-1$
|
||||
buffer.append("int SomeStructure;\n"); //$NON-NLS-1$
|
||||
String code = buffer.toString();
|
||||
|
||||
|
||||
for (ParserLanguage p = ParserLanguage.C; p != null; p = (p == ParserLanguage.C) ? ParserLanguage.CPP
|
||||
: null) {
|
||||
String filename = ( p == ParserLanguage.CPP ) ? "blah.cc" : "blah.c"; //$NON-NLS-1$ //$NON-NLS-2$
|
||||
String filename = (p == ParserLanguage.CPP) ? "blah.cc" : "blah.c"; //$NON-NLS-1$ //$NON-NLS-2$
|
||||
IFile source = importFile(filename, code); //$NON-NLS-1$
|
||||
IASTTranslationUnit tu = parse(source); //$NON-NLS-1$
|
||||
IASTSimpleDeclaration declaration = (IASTSimpleDeclaration) tu
|
||||
.getDeclarations()[0];
|
||||
assertSoleFileLocation(
|
||||
declaration.getDeclarators()[0],
|
||||
filename, code.indexOf("SomeStructure"), "SomeStructure".length()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
||||
assertSoleFileLocation(declaration.getDeclarators()[0],
|
||||
filename,
|
||||
code.indexOf("SomeStructure"), "SomeStructure".length()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class ParserConfiguration implements IParserConfiguration {
|
||||
|
||||
private final IScannerInfo info;
|
||||
|
||||
private final String dialect;
|
||||
|
||||
public ParserConfiguration(IScannerInfo s, IFile code) {
|
||||
this.info = s;
|
||||
String filename = code.getLocation().toOSString();
|
||||
IProject prj = code.getProject();
|
||||
|
||||
// FIXME: ALAIN, for headers should we assume CPP ??
|
||||
// The problem is that it really depends on how the header was
|
||||
// included.
|
||||
String id = null;
|
||||
IContentType contentType = CCorePlugin
|
||||
.getContentType(prj, filename);
|
||||
if (contentType != null) {
|
||||
id = contentType.getId();
|
||||
}
|
||||
|
||||
if (id != null) {
|
||||
if (CCorePlugin.CONTENT_TYPE_CSOURCE.equals(id))
|
||||
dialect = "GNUC"; //$NON-NLS-1$
|
||||
else
|
||||
dialect = "GNUC++"; //$NON-NLS-1$
|
||||
} else
|
||||
dialect = "GNUC++"; //$NON-NLS-1$
|
||||
|
||||
}
|
||||
|
||||
public IScannerInfo getScannerInfo() {
|
||||
return info;
|
||||
}
|
||||
|
||||
public String getParserDialect() {
|
||||
return dialect;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static final String[] EMPTY_STRING_ARRAY = new String[0];
|
||||
|
||||
/**
|
||||
|
@ -122,15 +159,19 @@ public class DOMLocationInclusionTests extends AST2FileBasePluginTest {
|
|||
super(name, DOMLocationInclusionTests.class);
|
||||
}
|
||||
|
||||
protected IASTTranslationUnit parse(IFile code)
|
||||
throws Exception {
|
||||
protected IASTTranslationUnit parse(IFile code) throws Exception {
|
||||
SavedCodeReaderFactory.getInstance().getCodeReaderCache().flush();
|
||||
return CDOM.getInstance().getTranslationUnit(code);
|
||||
}
|
||||
|
||||
protected IASTTranslationUnit parse(IFile code, IScannerInfo s) throws Exception {
|
||||
protected IASTTranslationUnit parse(IFile code, IScannerInfo s)
|
||||
throws Exception {
|
||||
SavedCodeReaderFactory.getInstance().getCodeReaderCache().flush();
|
||||
return CDOM.getInstance().getTranslationUnit( code, CDOM.getInstance().getCodeReaderFactory( CDOM.PARSE_SAVED_RESOURCES ), new ParserConfiguration( s, code ) );
|
||||
return CDOM.getInstance().getTranslationUnit(
|
||||
code,
|
||||
CDOM.getInstance().getCodeReaderFactory(
|
||||
CDOM.PARSE_SAVED_RESOURCES),
|
||||
new ParserConfiguration(s, code));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -155,19 +196,18 @@ public class DOMLocationInclusionTests extends AST2FileBasePluginTest {
|
|||
String code = "int bar;\n#include \"foo.h\"\n"; //$NON-NLS-1$
|
||||
|
||||
importFile("foo.h", foo); //$NON-NLS-1$
|
||||
|
||||
|
||||
for (ParserLanguage p = ParserLanguage.C; p != null; p = (p == ParserLanguage.C) ? ParserLanguage.CPP
|
||||
: null) {
|
||||
String filename = ( p == ParserLanguage.CPP ) ? "code.cc" : "code.c"; //$NON-NLS-1$ //$NON-NLS-2$
|
||||
String filename = (p == ParserLanguage.CPP) ? "code.cc" : "code.c"; //$NON-NLS-1$ //$NON-NLS-2$
|
||||
IFile cpp = importFile(filename, code); //$NON-NLS-1$
|
||||
IASTTranslationUnit tu = parse(cpp); //$NON-NLS-1$
|
||||
IASTDeclaration[] declarations = tu.getDeclarations();
|
||||
assertEquals(declarations.length, 2);
|
||||
IASTSimpleDeclaration bar = (IASTSimpleDeclaration) declarations[0];
|
||||
IASTSimpleDeclaration FOO = (IASTSimpleDeclaration) declarations[1];
|
||||
assertSoleFileLocation(bar,
|
||||
filename, code.indexOf("int"), code.indexOf(";") + 1); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
||||
assertSoleFileLocation(bar, filename,
|
||||
code.indexOf("int"), code.indexOf(";") + 1); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
||||
|
||||
assertSoleFileLocation(FOO,
|
||||
"foo.h", foo.indexOf("int"), foo.indexOf(";") + 1); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
||||
|
@ -176,7 +216,8 @@ public class DOMLocationInclusionTests extends AST2FileBasePluginTest {
|
|||
assertEquals(incs.length, 1);
|
||||
assertSoleFileLocation(
|
||||
incs[0],
|
||||
filename, 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$
|
||||
filename,
|
||||
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$
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -189,7 +230,7 @@ public class DOMLocationInclusionTests extends AST2FileBasePluginTest {
|
|||
|
||||
for (ParserLanguage p = ParserLanguage.C; p != null; p = (p == ParserLanguage.C) ? ParserLanguage.CPP
|
||||
: null) {
|
||||
String filename = ( p == ParserLanguage.CPP ) ? "code.cc" : "code.c"; //$NON-NLS-1$ //$NON-NLS-2$
|
||||
String filename = (p == ParserLanguage.CPP) ? "code.cc" : "code.c"; //$NON-NLS-1$ //$NON-NLS-2$
|
||||
IFile cpp = importFile(filename, code); //$NON-NLS-1$
|
||||
IASTTranslationUnit tu = parse(cpp); //$NON-NLS-1$
|
||||
IASTDeclaration[] declarations = tu.getDeclarations();
|
||||
|
@ -199,19 +240,22 @@ public class DOMLocationInclusionTests extends AST2FileBasePluginTest {
|
|||
IASTSimpleDeclaration byob = (IASTSimpleDeclaration) declarations[2];
|
||||
assertSoleFileLocation(
|
||||
bar,
|
||||
filename, code.indexOf("int"), code.indexOf("r;") + 2 - code.indexOf("int")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
|
||||
filename,
|
||||
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,
|
||||
filename, code.indexOf("float"), code.indexOf("b;") + 2 - code.indexOf("float")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
|
||||
filename,
|
||||
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],
|
||||
filename, 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$
|
||||
filename,
|
||||
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$
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -219,10 +263,10 @@ public class DOMLocationInclusionTests extends AST2FileBasePluginTest {
|
|||
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$
|
||||
|
||||
|
||||
for (ParserLanguage p = ParserLanguage.C; p != null; p = (p == ParserLanguage.C) ? ParserLanguage.CPP
|
||||
: null) {
|
||||
String filename = ( p == ParserLanguage.CPP ) ? "blah.cc" : "blah.c"; //$NON-NLS-1$ //$NON-NLS-2$
|
||||
String filename = (p == ParserLanguage.CPP) ? "blah.cc" : "blah.c"; //$NON-NLS-1$ //$NON-NLS-2$
|
||||
IFile c_file = importFile(filename, c_file_code); //$NON-NLS-1$
|
||||
IASTTranslationUnit tu = parse(c_file); //$NON-NLS-1$
|
||||
assertEquals(tu.getDeclarations().length, 0);
|
||||
|
@ -232,9 +276,10 @@ public class DOMLocationInclusionTests extends AST2FileBasePluginTest {
|
|||
assertEquals(macroDefinitions.length, 4);
|
||||
assertSoleFileLocation(
|
||||
macroDefinitions[0],
|
||||
filename, 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(),
|
||||
filename, c_file_code.indexOf("X"), 1); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
|
||||
filename,
|
||||
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(), filename,
|
||||
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$
|
||||
|
@ -248,10 +293,12 @@ public class DOMLocationInclusionTests extends AST2FileBasePluginTest {
|
|||
"blarg.h", h_file_code.indexOf("PRINT"), "PRINT".length()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
|
||||
assertSoleFileLocation(
|
||||
macroDefinitions[3],
|
||||
filename, c_file_code.indexOf("#define POST_INCLUDE"), "#define POST_INCLUDE".length()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
||||
filename,
|
||||
c_file_code.indexOf("#define POST_INCLUDE"), "#define POST_INCLUDE".length()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
||||
assertSoleFileLocation(
|
||||
macroDefinitions[3].getName(),
|
||||
filename, c_file_code.indexOf("POST_INCLUDE"), "POST_INCLUDE".length()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
||||
filename,
|
||||
c_file_code.indexOf("POST_INCLUDE"), "POST_INCLUDE".length()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -261,10 +308,10 @@ public class DOMLocationInclusionTests extends AST2FileBasePluginTest {
|
|||
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$
|
||||
|
||||
|
||||
for (ParserLanguage p = ParserLanguage.C; p != null; p = (p == ParserLanguage.C) ? ParserLanguage.CPP
|
||||
: null) {
|
||||
String filename = ( p == ParserLanguage.CPP ) ? "source.cc" : "source.c"; //$NON-NLS-1$ //$NON-NLS-2$
|
||||
String filename = (p == ParserLanguage.CPP) ? "source.cc" : "source.c"; //$NON-NLS-1$ //$NON-NLS-2$
|
||||
IFile f = importFile(filename, cpp_code); //$NON-NLS-1$
|
||||
IASTTranslationUnit tu = parse(f);
|
||||
IASTDeclaration[] declarations = tu.getDeclarations();
|
||||
|
@ -272,16 +319,18 @@ public class DOMLocationInclusionTests extends AST2FileBasePluginTest {
|
|||
.getIncludeDirectives();
|
||||
assertSoleFileLocation(
|
||||
includeDirectives[0],
|
||||
filename, cpp_code.indexOf("#include \"header1.h\""), "#include \"header1.h\"".length()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
||||
filename,
|
||||
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],
|
||||
filename, cpp_code.indexOf("#include \"header2.h\""), "#include \"header2.h\"".length()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
||||
assertSoleFileLocation(declarations[2],
|
||||
filename, cpp_code.indexOf("int z;"), "int z;".length()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
||||
filename,
|
||||
cpp_code.indexOf("#include \"header2.h\""), "#include \"header2.h\"".length()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
||||
assertSoleFileLocation(declarations[2], filename, cpp_code
|
||||
.indexOf("int z;"), "int z;".length()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
||||
|
||||
IASTTranslationUnit.IDependencyTree tree = tu.getDependencyTree();
|
||||
assertEquals(tree.getInclusions().length, 2);
|
||||
|
@ -295,10 +344,10 @@ public class DOMLocationInclusionTests extends AST2FileBasePluginTest {
|
|||
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$
|
||||
|
||||
|
||||
for (ParserLanguage p = ParserLanguage.C; p != null; p = (p == ParserLanguage.C) ? ParserLanguage.CPP
|
||||
: null) {
|
||||
String filename = ( p == ParserLanguage.CPP ) ? "blah.cc" : "blah.c"; //$NON-NLS-1$ //$NON-NLS-2$
|
||||
String filename = (p == ParserLanguage.CPP) ? "blah.cc" : "blah.c"; //$NON-NLS-1$ //$NON-NLS-2$
|
||||
IFile c_file = importFile(filename, c_file_code); //$NON-NLS-1$
|
||||
IASTTranslationUnit tu = parse(c_file); //$NON-NLS-1$
|
||||
assertEquals(tu.getDeclarations().length, 0);
|
||||
|
@ -308,9 +357,10 @@ public class DOMLocationInclusionTests extends AST2FileBasePluginTest {
|
|||
assertEquals(macroDefinitions.length, 6);
|
||||
assertSoleFileLocation(
|
||||
macroDefinitions[0],
|
||||
filename, 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(),
|
||||
filename, c_file_code.indexOf("X"), 1); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
|
||||
filename,
|
||||
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(), filename,
|
||||
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$
|
||||
|
@ -324,16 +374,19 @@ public class DOMLocationInclusionTests extends AST2FileBasePluginTest {
|
|||
"blarg.h", h_file_code.indexOf("PRINT"), "PRINT".length()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
|
||||
assertSoleFileLocation(
|
||||
macroDefinitions[3],
|
||||
filename, c_file_code.indexOf("#define POST_INCLUDE"), "#define POST_INCLUDE".length()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
||||
filename,
|
||||
c_file_code.indexOf("#define POST_INCLUDE"), "#define POST_INCLUDE".length()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
||||
assertSoleFileLocation(
|
||||
macroDefinitions[3].getName(),
|
||||
filename, c_file_code.indexOf("POST_INCLUDE"), "POST_INCLUDE".length()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
||||
filename,
|
||||
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],
|
||||
filename, c_file_code.indexOf("#define POST_SECOND"), "#define POST_SECOND".length()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
|
||||
filename,
|
||||
c_file_code.indexOf("#define POST_SECOND"), "#define POST_SECOND".length()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -357,10 +410,10 @@ public class DOMLocationInclusionTests extends AST2FileBasePluginTest {
|
|||
Collections.EMPTY_MAP, EMPTY_STRING_ARRAY, macros, includes);
|
||||
for (ParserLanguage p = ParserLanguage.C; p != null; p = (p == ParserLanguage.C) ? ParserLanguage.CPP
|
||||
: null) {
|
||||
String filename = ( p == ParserLanguage.CPP ) ? "main.cc" : "main.c"; //$NON-NLS-1$ //$NON-NLS-2$
|
||||
IFile code = importFile(
|
||||
filename, "int main() { return BEAST * sizeof( Include ); } "); //$NON-NLS-1$ //$NON-NLS-2$
|
||||
|
||||
String filename = (p == ParserLanguage.CPP) ? "main.cc" : "main.c"; //$NON-NLS-1$ //$NON-NLS-2$
|
||||
IFile code = importFile(filename,
|
||||
"int main() { return BEAST * sizeof( Include ); } "); //$NON-NLS-1$ //$NON-NLS-2$
|
||||
|
||||
IASTTranslationUnit tu = parse(code, scannerInfo); //$NON-NLS-1$
|
||||
IASTPreprocessorMacroDefinition[] macro_defs = tu
|
||||
.getMacroDefinitions();
|
||||
|
@ -387,10 +440,10 @@ public class DOMLocationInclusionTests extends AST2FileBasePluginTest {
|
|||
buffer.append("#include <not_found.h>\n"); //$NON-NLS-1$
|
||||
buffer.append("int x,y,z;"); //$NON-NLS-1$
|
||||
String code = buffer.toString();
|
||||
|
||||
|
||||
for (ParserLanguage p = ParserLanguage.C; p != null; p = (p == ParserLanguage.C) ? ParserLanguage.CPP
|
||||
: null) {
|
||||
String filename = ( p == ParserLanguage.CPP ) ? "blah.cc" : "blah.c"; //$NON-NLS-1$ //$NON-NLS-2$
|
||||
String filename = (p == ParserLanguage.CPP) ? "blah.cc" : "blah.c"; //$NON-NLS-1$ //$NON-NLS-2$
|
||||
IFile f = importFile(filename, code); //$NON-NLS-1$
|
||||
IASTTranslationUnit tu = parse(f); //$NON-NLS-1$
|
||||
IASTProblem[] prbs = tu.getPreprocessorProblems();
|
||||
|
@ -411,40 +464,6 @@ public class DOMLocationInclusionTests extends AST2FileBasePluginTest {
|
|||
return suite;
|
||||
}
|
||||
|
||||
// public void testBug97967() throws Exception {
|
||||
// IFile imacro_file = importFile(
|
||||
// "macro.h", "#define JEDEN 1\n#define DVA 2\n#define TRI 3\n"); //$NON-NLS-1$ //$NON-NLS-2$
|
||||
// StringBuffer buffer = new StringBuffer();
|
||||
// buffer.append("#ifndef _INCLUDE_H_\n"); //$NON-NLS-1$
|
||||
// buffer.append("#define _INCLUDE_H_\n"); //$NON-NLS-1$
|
||||
// buffer.append("typedef void (*vfp)();\n"); //$NON-NLS-1$
|
||||
// buffer.append("typedef int (*ifp)();\n"); //$NON-NLS-1$
|
||||
// buffer.append("struct Include {\n"); //$NON-NLS-1$
|
||||
// buffer.append("int i;\n"); //$NON-NLS-1$
|
||||
// buffer.append("};\n"); //$NON-NLS-1$
|
||||
// buffer.append("#endif /*_INCLUDE_H_*/\n"); //$NON-NLS-1$
|
||||
// final String inc_file_code = buffer.toString();
|
||||
// IFile include_file = importFile("include.h", inc_file_code); //$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);
|
||||
// String code = "int main() { return BEAST * sizeof( Include ); } "; //$NON-NLS-1$
|
||||
// for (ParserLanguage p = ParserLanguage.C; p != null; p = (p == ParserLanguage.C) ? ParserLanguage.CPP
|
||||
// : null) {
|
||||
// String filename = ( p == ParserLanguage.CPP ) ? "main.cc" : "main.c"; //$NON-NLS-1$ //$NON-NLS-2$
|
||||
// IFile c = importFile(
|
||||
// filename, code); //$NON-NLS-1$ //$NON-NLS-2$
|
||||
//
|
||||
// IASTTranslationUnit tu = parse(c, scannerInfo); //$NON-NLS-1$
|
||||
// IASTFunctionDefinition fd = (IASTFunctionDefinition) tu.getDeclarations()[3];
|
||||
// IASTFileLocation floc = fd.getFileLocation();
|
||||
// assertEquals( floc.getNodeOffset(), code.indexOf( "int main() { return BEAST * sizeof( Include ); }")); //$NON-NLS-1$
|
||||
// assertEquals( floc.getNodeLength(), "int main() { return BEAST * sizeof( Include ); }".length() ); //$NON-NLS-1$
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
public void testBug97603() throws Exception {
|
||||
IFile imacro_file = importFile(
|
||||
"macro.h", "#define JEDEN 1\n#define DVA 2\n#define TRI 3\n"); //$NON-NLS-1$ //$NON-NLS-2$
|
||||
|
@ -465,9 +484,9 @@ public class DOMLocationInclusionTests extends AST2FileBasePluginTest {
|
|||
Collections.EMPTY_MAP, EMPTY_STRING_ARRAY, macros, includes);
|
||||
for (ParserLanguage p = ParserLanguage.C; p != null; p = (p == ParserLanguage.C) ? ParserLanguage.CPP
|
||||
: null) {
|
||||
String filename = ( p == ParserLanguage.CPP ) ? "main.cc" : "main.c"; //$NON-NLS-1$ //$NON-NLS-2$
|
||||
IFile code = importFile(
|
||||
filename, "int main() { return BEAST * sizeof( Include ); } "); //$NON-NLS-1$ //$NON-NLS-2$
|
||||
String filename = (p == ParserLanguage.CPP) ? "main.cc" : "main.c"; //$NON-NLS-1$ //$NON-NLS-2$
|
||||
IFile code = importFile(filename,
|
||||
"int main() { return BEAST * sizeof( Include ); } "); //$NON-NLS-1$ //$NON-NLS-2$
|
||||
|
||||
IASTTranslationUnit tu = parse(code, scannerInfo); //$NON-NLS-1$
|
||||
IASTPreprocessorMacroDefinition[] macro_defs = tu
|
||||
|
|
|
@ -156,6 +156,7 @@ abstract class BaseScanner implements IScanner {
|
|||
protected Iterator preIncludeFiles = EmptyIterator.EMPTY_ITERATOR;
|
||||
|
||||
protected boolean isInitialized = false;
|
||||
protected boolean macroFilesInitialized = false;
|
||||
|
||||
protected final char[] suffixes;
|
||||
|
||||
|
@ -1259,6 +1260,7 @@ abstract class BaseScanner implements IScanner {
|
|||
if (info instanceof IExtendedScannerInfo) {
|
||||
extendedScannerInfoSetup(reader, info);
|
||||
} else {
|
||||
macroFilesInitialized = true;
|
||||
pushContext(reader.buffer, reader);
|
||||
isInitialized = true;
|
||||
}
|
||||
|
@ -1286,6 +1288,7 @@ abstract class BaseScanner implements IScanner {
|
|||
}
|
||||
}
|
||||
|
||||
macroFilesInitialized = true;
|
||||
if (einfo.getIncludeFiles() != null
|
||||
&& einfo.getIncludeFiles().length > 0)
|
||||
preIncludeFiles = Arrays.asList(einfo.getIncludeFiles()).iterator();
|
||||
|
|
|
@ -217,6 +217,11 @@ public class DOMScanner extends BaseScanner {
|
|||
bufferDelta[bufferStackPos + 1] = 0;
|
||||
}
|
||||
}
|
||||
else if( data instanceof CodeReader && !macroFilesInitialized )
|
||||
{
|
||||
int resolved = getGlobalCounter(0);
|
||||
locationMap.startInclusion( (CodeReader) data, resolved, resolved );
|
||||
}
|
||||
|
||||
super.pushContext(buffer, data);
|
||||
}
|
||||
|
@ -241,7 +246,10 @@ public class DOMScanner extends BaseScanner {
|
|||
locationMap.endTranslationUnit(bufferDelta[0]
|
||||
+ ((CodeReader) result).buffer.length);
|
||||
else
|
||||
{
|
||||
bufferDelta[0] += bufferDelta[bufferStackPos + 1] + ((CodeReader) result).buffer.length;
|
||||
locationMap.endInclusion( getGlobalCounter(0) );
|
||||
}
|
||||
|
||||
} else if (result instanceof InclusionData) {
|
||||
CodeReader codeReader = ((InclusionData) result).reader;
|
||||
|
|
Loading…
Add table
Reference in a new issue