Repository: dotnet/roslyn
File: src/Compilers/CSharp/Test/Symbol/Compilation/GetSemanticInfoTests.cs (C#)

Code before:
#endif

#define Z
";
            PreprocessingSymbolInfo symbolInfo = GetPreprocessingSymbolInfoForTest(sourceCode, "Z //bind");
            Assert.Equal("Z", symbolInfo.Symbol.Name);
            Assert.False(symbolInfo.IsDefined, "must not be defined");
        }

        [Fact, WorkItem(72907, "https://github.com/dotnet/roslyn/issues/72907")]
        public void GetPreprocessingSymbolInfoOnUnrelatedDirective()
        {
            const string sourceCode = """
                #define Z

                #region Z //bind region
                #endregion Z //bind endregion

                #pragma warning disable Z //bind pragma warning disable
                #warning Z //bind warning
                #error Z //bind error
                """;

            var compilation = CreateCompilation(sourceCode);
            var tree = compilation.SyntaxTrees[0];
            var model = compilation.GetSemanticModel(tree) as CSharpSemanticModel;

            var symbolInfo = symbolInfoAt("Z //bind region");
            Assert.Null(symbolInfo.Symbol);

            symbolInfo = symbolInfoAt("Z //bind endregion");
            Assert.Null(symbolInfo.Symbol);

            symbolInfo = symbolInfoAt("Z //bind pragma warning disable");
            Assert.Null(symbolInfo.Symbol);

            symbolInfo = symbolInfoAt("Z //bind warning");
            Assert.Null(symbolInfo.Symbol);

            symbolInfo = symbolInfoAt("Z //bind error");
            Assert.Null(symbolInfo.Symbol);

            PreprocessingSymbolInfo symbolInfoAt(string substringToMatch)
            {
                var position = sourceCode.IndexOf(substringToMatch, StringComparison.Ordinal);
                var nameSyntaxToBind = tree.GetRoot().FindToken(position, findInsideTrivia: true).Parent as IdentifierNameSyntax;

                if (nameSyntaxToBind is null)
                    return PreprocessingSymbolInfo.None;

                return model.GetPreprocessingSymbolInfo(nameSyntaxToBind);

Review comment (bug):
> as CSharpSemanticModel [](http://example.com/codeflow?start=58&length=23)

If conversion is not expected to fail, a regular cast syntax should be used. #Closed

Code after:
#endif

#define Z
";
            PreprocessingSymbolInfo symbolInfo = GetPreprocessingSymbolInfoForTest(sourceCode, "Z //bind");
            Assert.Equal("Z", symbolInfo.Symbol.Name);
            Assert.False(symbolInfo.IsDefined, "must not be defined");
        }

        [Fact, WorkItem(72907, "https://github.com/dotnet/roslyn/issues/72907")]
        public void GetPreprocessingSymbolInfoOnUnrelatedDirective()
        {
            const string sourceCode = """
                #pragma warning disable Z //bind pragma warning disable
                """;

            var symbolInfo = GetPreprocessingSymbolInfoForTest(sourceCode, "Z //bind pragma warning disable");
            Assert.Null(symbolInfo.Symbol);
        }

        [Fact, WorkItem(720566, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/720566")]
        public void Bug720566_01()
        {
            string sourceCode = @"
#define Z

#if Z //bind
#endif
";
            PreprocessingSymbolInfo symbolInfo = GetPreprocessingSymbolInfoForTest(sourceCode, "Z //bind");
            Assert.Equal("Z", symbolInfo.Symbol.Name);
            Assert.True(symbolInfo.IsDefined, "must be defined");
        }

        [Fact, WorkItem(720566, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/720566")]
        public void Bug720566_02()
        {
            string sourceCode = @"
#if true
    #define Z

    #if Z //bind
    #endif
#endif
";
            PreprocessingSymbolInfo symbolInfo = GetPreprocessingSymbolInfoForTest(sourceCode, "Z //bind");
            Assert.Equal("Z", symbolInfo.Symbol.Name);
            Assert.True(symbolInfo.IsDefined, "must be defined");
        }

        [Fact, WorkItem(720566, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/720566")]