[clang] modified AST for SEI redemption project (PR #111705)

2024-10-09 Thread Nicholas Reimer via cfe-commits

https://github.com/sei-nreimer created 
https://github.com/llvm/llvm-project/pull/111705

AST output modifications primarily focused on JSON enhancements for the SEI 
Redemption project. Some of the key changes are:

1. Recursive Pointer Resolution
- Modified the AST to accurately track multiple levels of pointer indirection 
during analysis

2. Function Pointer Identification
- Improved function pointer type identification handling, enabling correct 
resolution and analysis of function pointer assignments and calls within the AST

3. QualType Enhancements
- Updated QualType to better represent types with qualifiers such as `const`, 
`volatile`, and others

4. QualDetails Addition
- Introduced a new structure, QualDetails, to encapsulate additional metadata 
about type qualifiers, e.g., `ptr`, `signed`, `float`, `struct`, `union`, 
`array`, `promotable`, `integer`, `func_ptr`

5. Return Type Information
- Updated the AST to expose detailed return type information, similar to VarDecl

6. JSON Debloating:
- Reduced the size of JSON output by caching IDs and then using the `refID` key 
for referring back to the original ID

7. Added Missing Range and ID for CXXCtorInitializer

### Examples

```c
// recursive pointer resolution
int ** c;
```
![image](https://github.com/user-attachments/assets/e753bacb-67ec-47ae-b7a3-a24096cf6f31)

```c
// function pointer information
long (*foo)(int (*)(short));
```
![image](https://github.com/user-attachments/assets/e97abf8e-4acc-4716-8f34-2e0a80d0d94c)

```c
// return type information
int runFunctionTestA( char a );
```
![image](https://github.com/user-attachments/assets/bc348133-a778-4048-8eea-f81991c3e585)

```c
// refID usage example. 
int a; // (not in image) first time encountering an int
int *b; // (not in image) first time encountering int *, but second time 
encountering int
int **c;  // first time encountering int **, but second time encountering int * 
and third time int
```
![image](https://github.com/user-attachments/assets/77f7f6d0-be66-4618-9155-0bc7c8f3ff34)



>From 64ead42b291bd23bf47a37d6bcd6dea6b367e760 Mon Sep 17 00:00:00 2001
From: Nicholas Reimer 
Date: Tue, 8 Oct 2024 16:40:58 -0600
Subject: [PATCH] modified AST for SEI redemption project

---
 clang/include/clang/AST/ASTNodeTraverser.h |  59 +-
 clang/include/clang/AST/JSONNodeDumper.h   |  45 +++-
 clang/include/clang/AST/TextNodeDumper.h   |  22 
 clang/lib/AST/JSONNodeDumper.cpp   | 124 -
 clang/lib/AST/TextNodeDumper.cpp   |   8 ++
 clang/unittests/AST/ASTTraverserTest.cpp   |   4 +
 6 files changed, 252 insertions(+), 10 deletions(-)

diff --git a/clang/include/clang/AST/ASTNodeTraverser.h 
b/clang/include/clang/AST/ASTNodeTraverser.h
index a443a88bab1f2d..3299011771f332 100644
--- a/clang/include/clang/AST/ASTNodeTraverser.h
+++ b/clang/include/clang/AST/ASTNodeTraverser.h
@@ -11,6 +11,22 @@
 // similar to RecursiveASTVisitor.
 //
 
//===--===//
+//
+// Modifications to this file by SEI staff are copyright Carnegie Mellon
+// University and contributed under the Apache License v2.0 with LLVM
+// Exceptions.
+//
+// SEI Contributions are made with funding sand support from the Department of
+// Defense under Contract No. FA8702-15-D-0002 with Carnegie Mellon University
+// for the operation of the Software Engineering Institute, a federally funded
+// research and development center.
+//
+// The view, opinions, and/or findings contained in this material are those of
+// the author(s) and should not be construed as an official Government 
position,
+// policy, or decision, unless designated by other documentation.
+// DM24-0194
+//
+//===--===//
 
 #ifndef LLVM_CLANG_AST_ASTNODETRAVERSER_H
 #define LLVM_CLANG_AST_ASTNODETRAVERSER_H
@@ -177,14 +193,34 @@ class ASTNodeTraverser
 if (!SQT.Quals.hasQualifiers())
   return Visit(SQT.Ty);
 
-getNodeDelegate().AddChild([=] {
+// SEI: changed from default label to "qualTypeDetail"
+getNodeDelegate().AddChild("qualTypeDetail", [this, T] {
   getNodeDelegate().Visit(T);
   Visit(T.split().Ty);
 });
+
+// SEI function pointer support. this gets called whenever the three
+// conditions are met:
+// 1. the function pointer is not typedef'd
+// 2. after Visit(VarDecl *) gets called
+// 3. if VarDecl determines this is a function pointer
+if (T->isFunctionPointerType()) {
+  // create as a child node to this type
+  getNodeDelegate().AddChild(
+  [=] { getNodeDelegate().Visit(T->getPointeeType()); });
+}
+
+// SEI: traverse PointerType information
+if (T->isPointerType())
+  Visit(T->getPointeeType());
   }
 
+  // SEI: traverse ReturnType information
+  void VisitReturnType(QualType T) { getNodeDelegate().VisitReturnType(T); }
+
   void Visit(const Type *T) {
-getNodeDelegate().AddChil

[clang] modified AST for SEI redemption project (PR #111705)

2024-10-22 Thread Nicholas Reimer via cfe-commits


@@ -11,6 +11,22 @@
 // similar to RecursiveASTVisitor.
 //
 
//===--===//
+//
+// Modifications to this file by SEI staff are copyright Carnegie Mellon
+// University and contributed under the Apache License v2.0 with LLVM
+// Exceptions.
+//
+// SEI Contributions are made with funding sand support from the Department of
+// Defense under Contract No. FA8702-15-D-0002 with Carnegie Mellon University
+// for the operation of the Software Engineering Institute, a federally funded
+// research and development center.
+//
+// The view, opinions, and/or findings contained in this material are those of
+// the author(s) and should not be construed as an official Government 
position,
+// policy, or decision, unless designated by other documentation.
+// DM24-0194

sei-nreimer wrote:

Legal asked us to put those comments in originally. We're talking with the 
department now about removing them or putting them in a commit message. 

https://github.com/llvm/llvm-project/pull/111705
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits