Skip to content

[Draft] Summary Based Analysis Prototype #144224

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

Draft
wants to merge 30 commits into
base: main
Choose a base branch
from
Draft

Conversation

isuckatcs
Copy link
Member

This is the initial implementation of how I imagine summaries used in clang and the CSA. There are still rooms for improvement and the patch still serves as an RFC to discuss which direction to go further.

1.) Compile each TU to generate the summaries

// main.cpp
int *x;

void foo();

int main() {
  x = nullptr;
  foo();
  return *x;
}
// foo.cpp
void foo() {}
clang++ main.cpp foo.cpp --emit-summaries

2.) Give the path to the directory containing the summaries to a clang tool to use them

clang++ --analyze main.cpp --summaries-dir=.
main.cpp:8:10: warning: Dereference of null pointer (loaded from variable 'x') [core.NullDereference]
    8 |   return *x;
      |          ^~

The summaries for main.cpp and foo.cpp look like this:

[
  {
    "id": "c:@F@main#",
    "attrs": {
      "function": []
    },
    "calls": [
      {
        "id": "c:@F@foo#"
      }
    ]
  }
]
[
  {
    "id": "c:@F@foo#",
    "attrs": {
      "function": [
        "no_write_global"
      ]
    },
    "calls": []
  }
]

isuckatcs added 24 commits June 6, 2025 02:21
@isuckatcs isuckatcs requested review from Xazax-hun and steakhal June 14, 2025 14:04
Copy link

⚠️ We detected that you are using a GitHub private e-mail address to contribute to the repo.
Please turn off Keep my email addresses private setting in your account.
See LLVM Developer Policy and LLVM Discourse for more information.

Copy link

github-actions bot commented Jun 14, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

Copy link
Collaborator

@Xazax-hun Xazax-hun left a comment

Choose a reason for hiding this comment

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

I did not have time to look at it properly, just some small drive by comments.

return *SummaryCtx;
}

void createSummaryContext() { SummaryCtx.reset(new SummaryContext()); }
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should we assert here that we don't have a summary context yet?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not sure tbh. I just mirrored what other create functions such as CompilerInstance::createSema() do. Those don't assert the existence, so maybe the consumers of the class expect this kind of behaviour.


namespace clang {
class FunctionSummary {
SmallVector<char> ID;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why SmallVector instead of a string type?

Copy link
Member Author

Choose a reason for hiding this comment

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

This is just an artifact from index::generateUSRForDecl() expecting a SmallVectorImpl<char> & as it's buffer. In the very beginning, the constructor of this class took a FunctionDecl and generated the USR into this field.

This and every other data structure used needs to be revisited throught these source files once again before the patch is finalized.

FunctionSummary(SmallVector<char> ID, std::set<const SummaryAttr *> Attrs,
std::set<SmallVector<char>> Calls);

SmallVector<char> getID() const { return ID; }
Copy link
Collaborator

Choose a reason for hiding this comment

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

Did you want to return a StringRef/ArrayRef instead?

});
});
}
} // namespace clang
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: missing new line.

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

Successfully merging this pull request may close these issues.

2 participants