From 535d90cc7de7f976e71a81911664e0ac480d11e6 Mon Sep 17 00:00:00 2001 From: Markus Schorn Date: Fri, 7 Oct 2011 10:56:14 +0200 Subject: [PATCH] Option to record defined macros during expansion. --- .../core/parser/scanner/CPreprocessor.java | 17 ++++++++-------- .../core/parser/scanner/MacroExpander.java | 20 +++++++++++++------ 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/CPreprocessor.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/CPreprocessor.java index cd9fcc44a3c..4d20d06394f 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/CPreprocessor.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/CPreprocessor.java @@ -101,11 +101,12 @@ public class CPreprocessor implements ILexerLog, IScanner, IAdaptable { private static final DynamicMacro __LINE__ = new LineMacro("__LINE__".toCharArray()); //$NON-NLS-1$ private static final char[] ONCE = "once".toCharArray(); //$NON-NLS-1$ - private static final int NO_EXPANSION = 0x01; - private static final int PROTECT_DEFINED = 0x02; - private static final int STOP_AT_NL = 0x04; - private static final int CHECK_NUMBERS = 0x08; - private static final int REPORT_SIGNIFICANT_MACROS = 0x10; + static final int NO_EXPANSION = 0x01; + static final int PROTECT_DEFINED = 0x02; + static final int STOP_AT_NL = 0x04; + static final int CHECK_NUMBERS = 0x08; + static final int REPORT_SIGNIFICANT_MACROS = 0x10; + static final int IGNORE_UNDEFINED_SIGNIFICANT_MACROS = 0x20; private static final int MAX_INCLUSION_DEPTH = 200; @@ -1821,7 +1822,7 @@ public class CPreprocessor implements ILexerLog, IScanner, IAdaptable { } PreprocessorMacro macro= fMacroDictionary.get(name); if (macro == null) { - if (reportSignificant) + if ((options & IGNORE_UNDEFINED_SIGNIFICANT_MACROS) == 0) fCurrentContext.significantMacroUndefined(name); return false; } @@ -1843,9 +1844,7 @@ public class CPreprocessor implements ILexerLog, IScanner, IAdaptable { final ITokenSequence input= stopAtNewline ? fLineInputToMacroExpansion : fInputToMacroExpansion; final MacroExpander expander = withinExpansion ? new MacroExpander(this, fMacroDictionary, fLocationMap, fLexOptions) : fMacroExpander; - TokenList replacement= expander.expand(input, (options & PROTECT_DEFINED) != 0, macro, - identifier, contentAssist, - reportSignificant ? fCurrentContext : null); + TokenList replacement= expander.expand(input, options, macro, identifier, contentAssist, fCurrentContext); final IASTName[] expansions= expander.clearImplicitExpansions(); final ImageLocationInfo[] ili= expander.clearImageLocationInfos(); final Token last= replacement.last(); diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/MacroExpander.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/MacroExpander.java index bb16a6a7867..9667a1ea3cc 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/MacroExpander.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/MacroExpander.java @@ -137,6 +137,7 @@ public class MacroExpander { private int fFixedLineNumber; private char[] fFixedInput; private ScannerContext fReportMacros; + private boolean fReportUndefined; public MacroExpander(ILexerLog log, CharArrayMap macroDictionary, LocationMap locationMap, LexerOptions lexOptions) { fDictionary= macroDictionary; @@ -150,10 +151,17 @@ public class MacroExpander { * Expects that the identifier has been consumed, stores the result in the list provided. * @param scannerContext */ - public TokenList expand(ITokenSequence lexer, final boolean isPPCondition, + public TokenList expand(ITokenSequence lexer, final int ppOptions, PreprocessorMacro macro, Token identifier, boolean completionMode, ScannerContext scannerContext) throws OffsetLimitReachedException { - fReportMacros= scannerContext; + final boolean protectDefined= (ppOptions & CPreprocessor.PROTECT_DEFINED) != 0; + if ((ppOptions & CPreprocessor.REPORT_SIGNIFICANT_MACROS) != 0) { + fReportMacros= scannerContext; + fReportUndefined= (ppOptions & CPreprocessor.IGNORE_UNDEFINED_SIGNIFICANT_MACROS) == 0; + } else { + fReportMacros= null; + } + fImplicitMacroExpansions.clear(); fImageLocationInfos.clear(); @@ -175,7 +183,7 @@ public class MacroExpander { input.prepend(firstExpansion); - result= expandAll(input, forbidden, isPPCondition, null); + result= expandAll(input, forbidden, protectDefined, null); } catch (CompletionInMacroExpansionException e) { // for content assist in macro expansions, we return the list of tokens of the // parameter at the current cursor position and hope that they make sense if @@ -362,10 +370,10 @@ public class MacroExpander { } else if (macro == null || (macro.isFunctionStyle() && !input.findLParenthesis())) { // Tricky: Don't mark function-style macros if you don't find the left parenthesis if (fReportMacros != null) { - if (macro == null) { - fReportMacros.significantMacroUndefined(image); - } else { + if (macro != null) { fReportMacros.significantMacro(macro); + } else if (fReportUndefined){ + fReportMacros.significantMacroUndefined(image); } } result.append(t);