예제 #1
0
        internal bool IsPreprocessorSymbolDefined(string symbolName, int position)
        {
            if (preprocessorStateChangePositions.IsDefault)
            {
                BuildPreprocessorStateChangeMap();
            }

            int searchResult = preprocessorStateChangePositions.BinarySearch(position);

            Syntax.InternalSyntax.DirectiveStack directives;

            if (searchResult < 0)
            {
                searchResult = (~searchResult) - 1;

                if (searchResult >= 0)
                {
                    directives = preprocessorStates[searchResult];
                }
                else
                {
                    directives = Syntax.InternalSyntax.DirectiveStack.Empty;
                }
            }
            else
            {
                directives = preprocessorStates[searchResult];
            }

            return(IsPreprocessorSymbolDefined(directives, symbolName));
        }
예제 #2
0
        private void BuildPreprocessorStateChangeMap()
        {
            Syntax.InternalSyntax.DirectiveStack currentState = Syntax.InternalSyntax.DirectiveStack.Empty;
            var positions = ArrayBuilder <int> .GetInstance();

            var states = ArrayBuilder <Syntax.InternalSyntax.DirectiveStack> .GetInstance();

            foreach (DirectiveTriviaSyntax directive in this.GetRoot().GetDirectives(d =>
            {
                switch (d.CSharpKind())
                {
                case SyntaxKind.IfDirectiveTrivia:
                case SyntaxKind.ElifDirectiveTrivia:
                case SyntaxKind.ElseDirectiveTrivia:
                case SyntaxKind.EndIfDirectiveTrivia:
                case SyntaxKind.DefineDirectiveTrivia:
                case SyntaxKind.UndefDirectiveTrivia:
                    return(true);

                default:
                    return(false);
                }
            }))
            {
                currentState = directive.ApplyDirectives(currentState);

                switch (directive.CSharpKind())
                {
                case SyntaxKind.IfDirectiveTrivia:
                    // #if directive doesn't affect the set of defined/undefined symbols
                    break;

                case SyntaxKind.ElifDirectiveTrivia:
                    states.Add(currentState);
                    positions.Add(((ElifDirectiveTriviaSyntax)directive).ElifKeyword.SpanStart);
                    break;

                case SyntaxKind.ElseDirectiveTrivia:
                    states.Add(currentState);
                    positions.Add(((ElseDirectiveTriviaSyntax)directive).ElseKeyword.SpanStart);
                    break;

                case SyntaxKind.EndIfDirectiveTrivia:
                    states.Add(currentState);
                    positions.Add(((EndIfDirectiveTriviaSyntax)directive).EndIfKeyword.SpanStart);
                    break;

                case SyntaxKind.DefineDirectiveTrivia:
                    states.Add(currentState);
                    positions.Add(((DefineDirectiveTriviaSyntax)directive).Name.SpanStart);
                    break;

                case SyntaxKind.UndefDirectiveTrivia:
                    states.Add(currentState);
                    positions.Add(((UndefDirectiveTriviaSyntax)directive).Name.SpanStart);
                    break;

                default:
                    throw ExceptionUtilities.Unreachable;
                }
            }

#if DEBUG
            int currentPos = -1;
            foreach (int pos in positions)
            {
                Debug.Assert(currentPos < pos);
                currentPos = pos;
            }
#endif

            ImmutableInterlocked.InterlockedInitialize(ref preprocessorStates, states.ToImmutableAndFree());
            ImmutableInterlocked.InterlockedInitialize(ref preprocessorStateChangePositions, positions.ToImmutableAndFree());
        }
예제 #3
0
 internal void SetDirectiveStack(Syntax.InternalSyntax.DirectiveStack directives)
 {
     this.directives    = directives;
     this.hasDirectives = true;
 }