1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-23 14:42:11 +02:00

Bug 537942: Inactive include statements have wrong offsets

Fixed the bug and added a test for confirmation.

Change-Id: I5d3c5f797d84b10b961dcfc178a5bbcc3becbd04
Signed-off-by: Hansruedi Patzen <hansruedi.patzen@hsr.ch>
This commit is contained in:
Hansruedi Patzen 2018-08-14 17:44:30 +02:00 committed by Nathan Ridge
parent dd17366239
commit e69f72a488
3 changed files with 33 additions and 4 deletions

View file

@ -10,9 +10,10 @@
*******************************************************************************/
package org.eclipse.cdt.core.parser.tests.scanner;
import junit.framework.TestSuite;
import org.eclipse.cdt.core.parser.IToken;
import org.eclipse.cdt.internal.core.dom.parser.ASTNode;
import junit.framework.TestSuite;
/**
@ -275,4 +276,13 @@ public class InactiveCodeTests extends PreprocessorTestsBase {
validateProblemCount(0);
}
// #undef __INCLUDE_C_STD_LIB
// #ifdef __INCLUDE_C_STD_LIB
// #include <cstdint>
// #endif
public void testInactiveInclude_537942() throws Exception {
super.initializeScanner();
ASTNode include = (ASTNode) parse().getAllPreprocessorStatements()[2];
assertEquals(fCode.indexOf("#include <cstdint>"), include.getOffset());
}
}

View file

@ -13,9 +13,12 @@ package org.eclipse.cdt.core.parser.tests.scanner;
import java.io.IOException;
import org.eclipse.cdt.core.dom.ast.IASTProblem;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.IMacroBinding;
import org.eclipse.cdt.core.dom.parser.IScannerExtensionConfiguration;
import org.eclipse.cdt.core.dom.parser.c.GCCParserExtensionConfiguration;
import org.eclipse.cdt.core.dom.parser.c.GCCScannerExtensionConfiguration;
import org.eclipse.cdt.core.dom.parser.cpp.GPPParserExtensionConfiguration;
import org.eclipse.cdt.core.dom.parser.cpp.GPPScannerExtensionConfiguration;
import org.eclipse.cdt.core.parser.EndOfFileException;
import org.eclipse.cdt.core.parser.FileContent;
@ -31,6 +34,8 @@ import org.eclipse.cdt.core.parser.util.CharArrayUtils;
import org.eclipse.cdt.core.testplugin.CTestPlugin;
import org.eclipse.cdt.core.testplugin.util.BaseTestCase;
import org.eclipse.cdt.core.testplugin.util.TestSourceReader;
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.parser.scanner.CPreprocessor;
import org.eclipse.cdt.internal.core.parser.scanner.ILocationResolver;
@ -40,6 +45,7 @@ public abstract class PreprocessorTestsBase extends BaseTestCase {
private static final IParserLogService NULL_LOG = new NullLogService();
protected CPreprocessor fScanner;
protected ILocationResolver fLocationResolver;
protected String fCode;
public PreprocessorTestsBase(String name) {
super(name);
@ -66,6 +72,7 @@ public abstract class PreprocessorTestsBase extends BaseTestCase {
}
private FileContent getContent(String input) {
fCode = input;
return FileContent.create("<test-code>", input.toCharArray());
}
@ -93,6 +100,18 @@ public abstract class PreprocessorTestsBase extends BaseTestCase {
initializeScanner(getAboveComment());
}
protected IASTTranslationUnit parse() {
return parse(ParserLanguage.CPP);
}
protected IASTTranslationUnit parse(ParserLanguage lang) {
assertNotNull("The scanner needs to be initialized before parsing the code.", fScanner);
if (lang == ParserLanguage.C) {
return new GNUCSourceParser(fScanner, ParserMode.COMPLETE_PARSE, NULL_LOG, GCCParserExtensionConfiguration.getInstance()).parse();
}
return new GNUCPPSourceParser(fScanner, ParserMode.COMPLETE_PARSE, NULL_LOG, GPPParserExtensionConfiguration.getInstance()).parse();
}
protected StringBuilder[] getTestContent(int sections) throws IOException {
return TestSourceReader.getContentsForTest(
CTestPlugin.getDefault().getBundle(), "parser", getClass(), getName(), sections);
@ -198,7 +217,7 @@ public abstract class PreprocessorTestsBase extends BaseTestCase {
}
protected void validateAsUndefined(String name) {
assertNull(fScanner.getMacroDefinitions().get(name.toCharArray()));
assertNull(fScanner.getMacroDefinitions().get(name));
}
protected void validateProblemCount(int count) throws Exception {

View file

@ -1998,7 +1998,7 @@ public class CPreprocessor implements ILexerLog, IScanner, IAdaptable {
case IPreprocessorDirective.ppImport:
case IPreprocessorDirective.ppInclude:
case IPreprocessorDirective.ppInclude_next:
executeInclude(lexer, ident.getOffset(), type, false, withinExpansion);
executeInclude(lexer, pound.getOffset(), type, false, withinExpansion);
break;
case IPreprocessorDirective.ppIfdef:
return executeIfdef(lexer, pound.getOffset(), false, withinExpansion);