mirror of
https://github.com/github/codeql.git
synced 2025-12-17 01:03:14 +01:00
61 lines
2.0 KiB
C#
61 lines
2.0 KiB
C#
using Microsoft.CodeAnalysis;
|
|
using Microsoft.CodeAnalysis.CSharp;
|
|
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
|
using Semmle.Extraction.Entities;
|
|
using System.IO;
|
|
|
|
namespace Semmle.Extraction.CSharp.Entities
|
|
{
|
|
internal class UsingDirective : FreshEntity
|
|
{
|
|
private readonly UsingDirectiveSyntax Node;
|
|
private readonly NamespaceDeclaration Parent;
|
|
|
|
public UsingDirective(Context cx, UsingDirectiveSyntax usingDirective, NamespaceDeclaration parent)
|
|
: base(cx)
|
|
{
|
|
Node = usingDirective;
|
|
Parent = parent;
|
|
TryPopulate();
|
|
}
|
|
|
|
protected override void Populate(TextWriter trapFile)
|
|
{
|
|
var info = cx.GetModel(Node).GetSymbolInfo(Node.Name);
|
|
|
|
if (Node.StaticKeyword.Kind() == SyntaxKind.None)
|
|
{
|
|
// A normal using
|
|
if (info.Symbol is INamespaceSymbol namespaceSymbol)
|
|
{
|
|
var ns = Namespace.Create(cx, namespaceSymbol);
|
|
trapFile.using_namespace_directives(this, ns);
|
|
trapFile.using_directive_location(this, cx.Create(ReportingLocation));
|
|
}
|
|
else
|
|
{
|
|
cx.Extractor.MissingNamespace(Node.Name.ToFullString(), cx.FromSource);
|
|
cx.ModelError(Node, "Namespace not found");
|
|
return;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// A "using static"
|
|
var m = Type.Create(cx, (ITypeSymbol)info.Symbol);
|
|
trapFile.using_static_directives(this, m.TypeRef);
|
|
trapFile.using_directive_location(this, cx.Create(ReportingLocation));
|
|
}
|
|
|
|
if (Parent != null)
|
|
{
|
|
trapFile.parent_namespace_declaration(this, Parent);
|
|
}
|
|
}
|
|
|
|
public sealed override Microsoft.CodeAnalysis.Location ReportingLocation => Node.GetLocation();
|
|
|
|
public override TrapStackBehaviour TrapStackBehaviour => TrapStackBehaviour.NoLabel;
|
|
}
|
|
}
|