Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[c#] support for global using directives in CSharpProgramSummary #5187

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

xavierpinho
Copy link
Contributor

Adds support for handling global using directives. These are distinguished both during summarisation and in CSharpProgramSummary, so that findGlobalTypes accounts for them.

@xavierpinho xavierpinho added the c# Relates to csharpsrc2cpg label Dec 16, 2024
extends ProgramSummary[CSharpType, CSharpMethod, CSharpField] {

def findGlobalTypes: Set[CSharpType] = namespaceToType.getOrElse(Constants.Global, Set.empty).toSet
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I believe the original design choice was to assign globals to the global namespace. Does this not work for your use-case?

Copy link
Contributor Author

@xavierpinho xavierpinho Dec 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. From what I've seen, this Global namespace is being used to store top-level user-defined types found during pre-analysis. For using global XXX, we want to have XXX available in typesInScope but XXX is to be found in the program summary's NamespaceToTypeMap. Perhaps the best patch would be to add globalImports directly to typesInScope -- currently it's set to this findGlobalTypes. What do you reckon?

To illustrate, the newer patch I'm proposing would look something like (in CSharpScope.scala):

  override val typesInScope: mutable.Set[CSharpType] = {
    val globalTypes = summary.findGlobalTypes
    val globallyImportedTypes = summary.globalImports.flatMap(summary.namespaceToType.getOrElse(_, Set.empty))
    val result = mutable.Set.empty[CSharpType]
    result.addAll(globalTypes)
    result.addAll(globallyImportedTypes)
    result
  }

EDIT: See 3e016b4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c# Relates to csharpsrc2cpg
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants