From e83f41c613e09d6c53cc447a48c2d3d705f86e7c Mon Sep 17 00:00:00 2001 From: John Camelon Date: Wed, 6 Apr 2005 19:24:37 +0000 Subject: [PATCH] Fixed Bug 87894 - [Offset] example from windows.h generates weird AST in DOM View --- .../tests/ast2/DOMLocationInclusionTests.java | 38 +++---------------- .../core/parser/scanner2/BaseScanner.java | 37 +++++++++--------- .../core/parser/scanner2/DOMScanner.java | 12 +++--- .../core/parser/scanner2/InclusionNode.java | 4 ++ .../core/parser/scanner2/LocationMap.java | 5 +++ 5 files changed, 40 insertions(+), 56 deletions(-) diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMLocationInclusionTests.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMLocationInclusionTests.java index d753e773f5a..5e8f9efc205 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMLocationInclusionTests.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMLocationInclusionTests.java @@ -21,11 +21,15 @@ import org.eclipse.cdt.core.dom.ast.IASTDeclaration; import org.eclipse.cdt.core.dom.ast.IASTFileLocation; import org.eclipse.cdt.core.dom.ast.IASTNode; 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.IASTPreprocessorMacroDefinition; +import org.eclipse.cdt.core.dom.ast.IASTPreprocessorStatement; import org.eclipse.cdt.core.dom.ast.IASTProblem; import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration; 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.IParserLogService; import org.eclipse.cdt.core.parser.IScanner; @@ -305,40 +309,8 @@ public class DOMLocationInclusionTests extends FileBasePluginTest { 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 testBug87894() throws Exception { - StringBuffer fakeBuffer = new StringBuffer( "// windows_fake.h:\n" ); //$NON-NLS-1$ - fakeBuffer.append( "#ifdef RC_INVOKED\n" ); //$NON-NLS-1$ - fakeBuffer.append( "#define WIN32_LEAN_AND_MEAN\n" ); //$NON-NLS-1$ - fakeBuffer.append( "#endif\n" ); //$NON-NLS-1$ - fakeBuffer.append( "\n" ); //$NON-NLS-1$ - fakeBuffer.append( "#ifndef WIN32_LEAN_AND_MEAN\n" ); //$NON-NLS-1$ - fakeBuffer.append( "#include \"rpc_fake.h\"\n" ); //$NON-NLS-1$ - fakeBuffer.append( "#endif)\n" ); //$NON-NLS-1$ - String windows_fake_h_code = fakeBuffer.toString(); - importFile( "windows_fake.h", windows_fake_h_code ); //$NON-NLS-1$ - StringBuffer rpcBuffer = new StringBuffer(); - rpcBuffer.append( "// rpc_fake.h:\n" ); //$NON-NLS-1$ - rpcBuffer.append( "#define RC_INVOKED\n" ); //$NON-NLS-1$ - rpcBuffer.append( "\n" ); //$NON-NLS-1$ - rpcBuffer.append( "#ifndef RPC_NO_WINDOWS_H\n" ); //$NON-NLS-1$ - rpcBuffer.append( "#include \"windows_fake.h\"\n" ); //$NON-NLS-1$ - rpcBuffer.append( "#endif\n" ); //$NON-NLS-1$ - String rpc_fake_h_code = rpcBuffer.toString(); - importFile( "rpc_fake.h", rpc_fake_h_code ); //$NON-NLS-1$ - String test_c_code = "// test.c:\n#include \"windows_fake.h\"\n // endOffset is 64 but should be more like 266"; //$NON-NLS-1$ - IFile f = importFile( "test.c", test_c_code ); //$NON-NLS-1$ - for (ParserLanguage p = ParserLanguage.C; p != null; p = (p == ParserLanguage.C) ? ParserLanguage.CPP - : null) { - IASTTranslationUnit tu = parse(f, p); //$NON-NLS-1$ - - } - - - - } public static Test suite() { diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/BaseScanner.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/BaseScanner.java index f656744a8f2..fbd94642e83 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/BaseScanner.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/BaseScanner.java @@ -1344,24 +1344,7 @@ abstract class BaseScanner implements IScanner { protected void pushContext(char[] buffer, Object data) { if (data instanceof InclusionData) { - boolean isCircular = false; - for (int i = 0; i < bufferStackPos; ++i) { - if (bufferData[i] instanceof CodeReader - && CharArrayUtils.equals( - ((CodeReader) bufferData[i]).filename, - ((InclusionData) data).reader.filename)) { - isCircular = true; - break; - } else if (bufferData[i] instanceof InclusionData - && CharArrayUtils - .equals( - ((InclusionData) bufferData[i]).reader.filename, - ((InclusionData) data).reader.filename)) { - isCircular = true; - break; - } - } - if (isCircular) + if (isCircularInclusion( (InclusionData)data )) return; } pushContext(buffer); @@ -1369,6 +1352,24 @@ abstract class BaseScanner implements IScanner { } + protected boolean isCircularInclusion(InclusionData data) { + for (int i = 0; i < bufferStackPos; ++i) { + if (bufferData[i] instanceof CodeReader + && CharArrayUtils.equals( + ((CodeReader) bufferData[i]).filename, + data.reader.filename)) { + return true; + } else if (bufferData[i] instanceof InclusionData + && CharArrayUtils + .equals( + ((InclusionData) bufferData[i]).reader.filename, + data.reader.filename)) { + return true; + } + } + return false; + } + protected Object popContext() { //NOTE - do not set counters to 0 or -1 or something //Subclasses may require those values in their popContext() diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/DOMScanner.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/DOMScanner.java index 748fca641a7..10363aeadb2 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/DOMScanner.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/DOMScanner.java @@ -187,11 +187,13 @@ public class DOMScanner extends BaseScanner { b.append(((InclusionData) data).reader.filename); log.traceLog(b.toString()); } - - DOMInclusion inc = ((DOMInclusion) ((InclusionData) data).inclusion); - locationMap.startInclusion(((InclusionData) data).reader, inc.o, - resolveOffset(getCurrentOffset())); - bufferDelta[bufferStackPos + 1] = 0; + if( ! isCircularInclusion( (InclusionData) data )) + { + DOMInclusion inc = ((DOMInclusion) ((InclusionData) data).inclusion); + locationMap.startInclusion(((InclusionData) data).reader, inc.o, + resolveOffset(getCurrentOffset())); + bufferDelta[bufferStackPos + 1] = 0; + } } else if (data instanceof MacroData) { diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/InclusionNode.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/InclusionNode.java index d066e67bb26..18e8146edcd 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/InclusionNode.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/InclusionNode.java @@ -36,5 +36,9 @@ public class InclusionNode implements IASTInclusionNode, IDependencyNodeHost { public void addInclusionNode(IASTInclusionNode node) { incs = (IASTInclusionNode[]) ArrayUtil.append( IASTInclusionNode.class, incs, node ); } + + public String toString() { + return stmt.toString(); + } } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/LocationMap.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/LocationMap.java index 973e0b278d4..d359545408b 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/LocationMap.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/LocationMap.java @@ -457,6 +457,11 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { public String getPath() { return new String(path); } + + public String toString() { + return getPath(); + } + }