Exemplo n.º 1
0
        private DirectiveTriviaSyntax GetPreviousPossiblyRelatedDirective()
        {
            DirectiveTriviaSyntax d = this;

            while (d != null)
            {
                d = d.GetPreviousDirective();
                if (d != null)
                {
                    // skip matched sets
                    switch (d.Kind)
                    {
                    case SyntaxKind.EndIfDirectiveTrivia:
                        while (d != null && d.Kind != SyntaxKind.IfDirectiveTrivia)
                        {
                            d = d.GetPreviousRelatedDirective();
                        }

                        continue;

                    case SyntaxKind.EndRegionDirectiveTrivia:
                        while (d != null && d.Kind != SyntaxKind.RegionDirectiveTrivia)
                        {
                            d = d.GetPreviousRelatedDirective();
                        }

                        continue;
                    }
                }

                return(d);
            }

            return(null);
        }
Exemplo n.º 2
0
        private void ClassifyPreprocessorDirective(DirectiveTriviaSyntax node)
        {
            if (!_textSpan.OverlapsWith(node.Span))
            {
                return;
            }

            switch (node.Kind())
            {
                case SyntaxKind.IfDirectiveTrivia:
                    ClassifyIfDirective((IfDirectiveTriviaSyntax)node);
                    break;
                case SyntaxKind.ElifDirectiveTrivia:
                    ClassifyElifDirective((ElifDirectiveTriviaSyntax)node);
                    break;
                case SyntaxKind.ElseDirectiveTrivia:
                    ClassifyElseDirective((ElseDirectiveTriviaSyntax)node);
                    break;
                case SyntaxKind.EndIfDirectiveTrivia:
                    ClassifyEndIfDirective((EndIfDirectiveTriviaSyntax)node);
                    break;
                case SyntaxKind.RegionDirectiveTrivia:
                    ClassifyRegionDirective((RegionDirectiveTriviaSyntax)node);
                    break;
                case SyntaxKind.EndRegionDirectiveTrivia:
                    ClassifyEndRegionDirective((EndRegionDirectiveTriviaSyntax)node);
                    break;
                case SyntaxKind.ErrorDirectiveTrivia:
                    ClassifyErrorDirective((ErrorDirectiveTriviaSyntax)node);
                    break;
                case SyntaxKind.WarningDirectiveTrivia:
                    ClassifyWarningDirective((WarningDirectiveTriviaSyntax)node);
                    break;
                case SyntaxKind.BadDirectiveTrivia:
                    ClassifyBadDirective((BadDirectiveTriviaSyntax)node);
                    break;
                case SyntaxKind.DefineDirectiveTrivia:
                    ClassifyDefineDirective((DefineDirectiveTriviaSyntax)node);
                    break;
                case SyntaxKind.UndefDirectiveTrivia:
                    ClassifyUndefDirective((UndefDirectiveTriviaSyntax)node);
                    break;
                case SyntaxKind.LineDirectiveTrivia:
                    ClassifyLineDirective((LineDirectiveTriviaSyntax)node);
                    break;
                case SyntaxKind.PragmaChecksumDirectiveTrivia:
                    ClassifyPragmaChecksumDirective((PragmaChecksumDirectiveTriviaSyntax)node);
                    break;
                case SyntaxKind.PragmaWarningDirectiveTrivia:
                    ClassifyPragmaWarningDirective((PragmaWarningDirectiveTriviaSyntax)node);
                    break;
                case SyntaxKind.ReferenceDirectiveTrivia:
                    ClassifyReferenceDirective((ReferenceDirectiveTriviaSyntax)node);
                    break;
                case SyntaxKind.LoadDirectiveTrivia:
                    ClassifyLoadDirective((LoadDirectiveTriviaSyntax)node);
                    break;
            }
        }
Exemplo n.º 3
0
 private static bool IsActiveConditionalDirective(DirectiveTriviaSyntax directive)
 {
     switch (directive.Kind())
     {
         case SyntaxKind.DefineDirectiveTrivia:
             return ((DefineDirectiveTriviaSyntax)directive).IsActive;
         case SyntaxKind.UndefDirectiveTrivia:
             return ((UndefDirectiveTriviaSyntax)directive).IsActive;
         default:
             return false;
     }
 }
Exemplo n.º 4
0
        private static bool IsActiveConditionalDirective(DirectiveTriviaSyntax directive)
        {
            switch (directive.Kind())
            {
            case SyntaxKind.DefineDirectiveTrivia:
                return(((DefineDirectiveTriviaSyntax)directive).IsActive);

            case SyntaxKind.UndefDirectiveTrivia:
                return(((UndefDirectiveTriviaSyntax)directive).IsActive);

            default:
                return(false);
            }
        }
Exemplo n.º 5
0
            private static bool HasRelatedDirectives(DirectiveTriviaSyntax directive)
            {
                switch (directive.Kind())
                {
                case SyntaxKind.IfDirectiveTrivia:
                case SyntaxKind.ElseDirectiveTrivia:
                case SyntaxKind.ElifDirectiveTrivia:
                case SyntaxKind.EndIfDirectiveTrivia:
                case SyntaxKind.RegionDirectiveTrivia:
                case SyntaxKind.EndRegionDirectiveTrivia:
                    return(true);

                default:
                    return(false);
                }
            }
Exemplo n.º 6
0
        private void ClassifyDirectiveTrivia(DirectiveTriviaSyntax node, bool allowComments = true)
        {
            var lastToken = node.EndOfDirectiveToken.GetPreviousToken(includeSkipped: false);

            foreach (var trivia in lastToken.TrailingTrivia)
            {
                // skip initial whitespace
                if (trivia.CSharpKind() == SyntaxKind.WhitespaceTrivia)
                {
                    continue;
                }

                ClassifyPreprocessorTrivia(trivia, allowComments);
            }

            foreach (var trivia in node.EndOfDirectiveToken.LeadingTrivia)
            {
                ClassifyPreprocessorTrivia(trivia, allowComments);
            }
        }
Exemplo n.º 7
0
 private void TestShebang(DirectiveTriviaSyntax directive, string expectedSkippedText)
 {
     var shebang = (ShebangDirectiveTriviaSyntax)directive;
     Assert.False(shebang.HasStructuredTrivia);
     Assert.Equal(SyntaxKind.HashToken, shebang.HashToken.Kind());
     Assert.Equal(SyntaxKind.ExclamationToken, shebang.ExclamationToken.Kind());
     var endOfDirective = shebang.EndOfDirectiveToken;
     Assert.Equal(SyntaxKind.EndOfDirectiveToken, endOfDirective.Kind());
     Assert.Equal(0, endOfDirective.Span.Length);
     var skippedText = endOfDirective.LeadingTrivia.Single();
     Assert.Equal(SyntaxKind.PreprocessingMessageTrivia, skippedText.Kind());
     Assert.Equal(expectedSkippedText, skippedText.ToString());
 }
            private void FinishIf(DirectiveTriviaSyntax directiveOpt)
            {
                var condDirectives = new List<DirectiveTriviaSyntax>();
                if (directiveOpt != null)
                {
                    condDirectives.Add(directiveOpt);
                }

                while (!_ifStack.IsEmpty())
                {
                    var poppedDirective = _ifStack.Pop();
                    condDirectives.Add(poppedDirective);
                    if (poppedDirective.Kind() == SyntaxKind.IfDirectiveTrivia)
                    {
                        break;
                    }
                }

                condDirectives.Sort((n1, n2) => n1.SpanStart.CompareTo(n2.SpanStart));

                foreach (var cond in condDirectives)
                {
                    _conditionalMap.Add(cond, condDirectives);
                }

                // #If should be the first one in sorted order
                var ifDirective = condDirectives.First();
                Contract.Assert(
                    ifDirective.Kind() == SyntaxKind.IfDirectiveTrivia ||
                    ifDirective.Kind() == SyntaxKind.ElifDirectiveTrivia ||
                    ifDirective.Kind() == SyntaxKind.ElseDirectiveTrivia);

                if (directiveOpt != null)
                {
                    _directiveMap.Add(directiveOpt, ifDirective);
                    _directiveMap.Add(ifDirective, directiveOpt);
                }
            }
 private void HandleElseDirective(DirectiveTriviaSyntax directive)
 {
     ifStack.Push(directive);
 }
 private Tristate EvaluateDirectiveExpression(DirectiveTriviaSyntax directive, Tristate previousRegionState)
 {
     switch (directive.Kind())
     {
         case SyntaxKind.IfDirectiveTrivia:
             return EvaluateExpression(((IfDirectiveTriviaSyntax)directive).Condition);
         case SyntaxKind.ElifDirectiveTrivia:
             Tristate result = EvaluateExpression(((ElifDirectiveTriviaSyntax)directive).Condition);
             return !previousRegionState & result;
         case SyntaxKind.ElseDirectiveTrivia:
             return !previousRegionState;
         default:
             Debug.Assert(false);
             return Tristate.Varying;
     }
 }
            private void HandleEndRegionDirective(DirectiveTriviaSyntax directive)
            {
                if (regionStack.IsEmpty())
                {
                    return;
                }

                var previousDirective = regionStack.Pop();

                directiveMap.Add(directive, previousDirective);
                directiveMap.Add(previousDirective, directive);
            }
 private void HandleRegionDirective(DirectiveTriviaSyntax directive)
 {
     regionStack.Push(directive);
 }
        private static string GetReplacementText(DirectiveTriviaSyntax startDirective, DirectiveTriviaSyntax endDirective)
        {
            if (startDirective.Kind() == SyntaxKind.IfDirectiveTrivia && endDirective.Kind() == SyntaxKind.ElifDirectiveTrivia)
            {
                var elifDirective = (ElifDirectiveTriviaSyntax)endDirective;
                var elifKeyword = elifDirective.ElifKeyword;
                var newIfDirective = SyntaxFactory.IfDirectiveTrivia(
                    elifDirective.HashToken,
                    SyntaxFactory.Token(elifKeyword.LeadingTrivia, SyntaxKind.IfKeyword, "if", "if", elifKeyword.TrailingTrivia),
                    elifDirective.Condition,
                    elifDirective.EndOfDirectiveToken,
                    elifDirective.IsActive,
                    elifDirective.BranchTaken,
                    elifDirective.ConditionValue);

                return newIfDirective.ToFullString();
            }
            else
            {
                return endDirective.ToFullString();
            }
        }
            private void HandleEndIfDirective(DirectiveTriviaSyntax directive)
            {
                if (ifStack.IsEmpty())
                {
                    return;
                }

                var condDirectives = new List<DirectiveTriviaSyntax>();
                condDirectives.Add(directive);

                while (!ifStack.IsEmpty())
                {
                    var poppedDirective = ifStack.Pop();
                    condDirectives.Add(poppedDirective);
                    if (poppedDirective.CSharpKind() == SyntaxKind.IfDirectiveTrivia)
                    {
                        break;
                    }
                }

                condDirectives.Sort((n1, n2) => n1.SpanStart.CompareTo(n2.SpanStart));

                foreach (var cond in condDirectives)
                {
                    conditionalMap.Add(cond, condDirectives);
                }

                // #If should be the first one in sorted order
                var ifDirective = condDirectives.First();
                Contract.Assert(
                    ifDirective.CSharpKind() == SyntaxKind.IfDirectiveTrivia ||
                    ifDirective.CSharpKind() == SyntaxKind.ElifDirectiveTrivia ||
                    ifDirective.CSharpKind() == SyntaxKind.ElseDirectiveTrivia);

                directiveMap.Add(directive, ifDirective);
                directiveMap.Add(ifDirective, directive);
            }
 private void HandleIfDirective(DirectiveTriviaSyntax directive)
 {
     _ifStack.Push(directive);
 }
            private void HandleEndIfDirective(DirectiveTriviaSyntax directive)
            {
                if (_ifStack.IsEmpty())
                {
                    return;
                }

                FinishIf(directive);
            }
Exemplo n.º 17
0
        private DirectiveTriviaSyntax GetPreviousRelatedDirective()
        {
            DirectiveTriviaSyntax d = this;

            switch (d.Kind)
            {
            case SyntaxKind.EndIfDirectiveTrivia:
                while (d != null)
                {
                    switch (d.Kind)
                    {
                    case SyntaxKind.IfDirectiveTrivia:
                    case SyntaxKind.ElifDirectiveTrivia:
                    case SyntaxKind.ElseDirectiveTrivia:
                        return(d);
                    }

                    d = d.GetPreviousPossiblyRelatedDirective();
                }

                break;

            case SyntaxKind.ElifDirectiveTrivia:
                while (d != null)
                {
                    if (d.Kind == SyntaxKind.IfDirectiveTrivia)
                    {
                        return(d);
                    }

                    d = d.GetPreviousPossiblyRelatedDirective();
                }

                break;

            case SyntaxKind.ElseDirectiveTrivia:
                while (d != null)
                {
                    switch (d.Kind)
                    {
                    case SyntaxKind.IfDirectiveTrivia:
                    case SyntaxKind.ElifDirectiveTrivia:
                        return(d);
                    }

                    d = d.GetPreviousPossiblyRelatedDirective();
                }

                break;

            case SyntaxKind.EndRegionDirectiveTrivia:
                while (d != null)
                {
                    if (d.Kind == SyntaxKind.RegionDirectiveTrivia)
                    {
                        return(d);
                    }

                    d = d.GetPreviousPossiblyRelatedDirective();
                }

                break;
            }

            return(null);
        }
 private static bool IsBranchingDirective(DirectiveTriviaSyntax directive)
 {
     switch (directive.Kind())
     {
         case SyntaxKind.IfDirectiveTrivia:
         case SyntaxKind.ElifDirectiveTrivia:
         case SyntaxKind.ElseDirectiveTrivia:
             return true;
         default:
             return false;
     }
 }
 private DirectiveTriviaSyntax SimplifyDirectiveExpression(DirectiveTriviaSyntax directive)
 {
     switch (directive.Kind())
     {
         case SyntaxKind.IfDirectiveTrivia:
             {
                 var ifDirective = (IfDirectiveTriviaSyntax)directive;
                 return ifDirective.WithCondition((ExpressionSyntax)ifDirective.Condition.Accept(_expressionSimplifier));
             }
         case SyntaxKind.ElifDirectiveTrivia:
             {
                 var elifDirective = (ElifDirectiveTriviaSyntax)directive;
                 return elifDirective.WithCondition((ExpressionSyntax)elifDirective.Condition.Accept(_expressionSimplifier));
             }
         case SyntaxKind.ElseDirectiveTrivia:
             return directive;
         default:
             Debug.Assert(false);
             return null;
     }
 }