[clang] [llvm] Expose format (attribute) info for function declarations in Clang Index API (PR #113754)

2024-10-25 Thread AR Visions via cfe-commits

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


[clang] [llvm] Enable format info for C function declarations in Clang Index API, so we may have this context (PR #113754)

2024-10-25 Thread AR Visions via cfe-commits

https://github.com/ar-visions created 
https://github.com/llvm/llvm-project/pull/113754

Enable format info for C function declarations in Clang Index API, so we may 
have this context

clang_Cursor_getFormatAttr
Get FormatAttr at Function Declaration

clang_FormatAttr_getType
Get the format-type field for FormatAttr (i.e. printf, scanf)

clang_FormatAttr_getFormatIdx
Get the arg index of format template C-string

clang_FormatAttr_getFirstArg
Get the arg index of first arg for formatting params


The purpose of this is to enable the silver compiler (LLVM IR C API) to know 
context information on imported C functions, so it may convert arguments with 
more context, and no manual tables describing this information.  This leverages 
LLVM's FormatAttr data that is already present so Clang Index users may parse C 
more effectively

>From d37feb71e88adbfe6eabad94cbe0d83bb19a5fd1 Mon Sep 17 00:00:00 2001
From: Kalen Novis White 
Date: Fri, 25 Oct 2024 22:05:09 -0700
Subject: [PATCH] Add following functions to Clang Index API, enabling
 formatter information when parsing headers: clang_Cursor_getFormatAttr 
 Get FormatAttr at Function Declaration

clang_FormatAttr_getType
Get the format-type field for FormatAttr (i.e. printf, scanf)

clang_FormatAttr_getFormatIdx
Get the arg index of format template C-string

clang_FormatAttr_getFirstArg
Get the arg index of first arg for formatting params
---
 .gitignore|  2 ++
 clang/include/clang-c/Index.h | 23 +++
 clang/tools/libclang/CXCursor.cpp | 26 ++
 clang/tools/libclang/libclang.map |  4 
 4 files changed, 55 insertions(+)

diff --git a/.gitignore b/.gitignore
index 0e7c6c79001338..61de1404c127d6 100644
--- a/.gitignore
+++ b/.gitignore
@@ -71,3 +71,5 @@ pythonenv*
 /clang/utils/analyzer/projects/*/RefScanBuildResults
 # automodapi puts generated documentation files here.
 /lldb/docs/python_api/
+silver-debug
+silver-build
diff --git a/clang/include/clang-c/Index.h b/clang/include/clang-c/Index.h
index 0c5ac80772e2b9..d27c571b776405 100644
--- a/clang/include/clang-c/Index.h
+++ b/clang/include/clang-c/Index.h
@@ -3109,6 +3109,29 @@ CINDEX_LINKAGE int clang_getFieldDeclBitWidth(CXCursor 
C);
  */
 CINDEX_LINKAGE int clang_Cursor_getNumArguments(CXCursor C);
 
+/**
+ * Retrieve FormatAttr on function declaration
+ */
+CINDEX_LINKAGE CXCursor clang_Cursor_getFormatAttr   (CXCursor cur);
+
+/**
+ * Retrieve string name of the type of formatting (i.e. scanf or printf)
+ * used with clang_Cursor_getFormatAttr
+ */
+CINDEX_LINKAGE CXString clang_FormatAttr_getType (CXCursor C);
+
+/**
+ * Retrieve the arg location (0-based) of the template C-string
+ * used with clang_Cursor_getFormatAttr
+ */
+CINDEX_LINKAGE unsigned clang_FormatAttr_getFormatIdx(CXCursor C);
+
+/**
+ * Retrieve the arg location to the first format argument
+ * used with clang_Cursor_getFormatAttr
+ */
+CINDEX_LINKAGE unsigned clang_FormatAttr_getFirstArg (CXCursor C);
+
 /**
  * Retrieve the argument cursor of a function or method.
  *
diff --git a/clang/tools/libclang/CXCursor.cpp 
b/clang/tools/libclang/CXCursor.cpp
index 562228cc334f3a..1ec0dc05123193 100644
--- a/clang/tools/libclang/CXCursor.cpp
+++ b/clang/tools/libclang/CXCursor.cpp
@@ -1330,6 +1330,32 @@ CXTranslationUnit 
clang_Cursor_getTranslationUnit(CXCursor cursor) {
   return getCursorTU(cursor);
 }
 
+CXCursor clang_Cursor_getFormatAttr(CXCursor cur) {
+  const Decl *decl = cxcursor::getCursorDecl(cur);
+  if (const FunctionDecl *fd = dyn_cast_or_null(decl))
+if (const FormatAttr *fa = fd->getAttr())
+  return cxcursor::MakeCXCursor(fa, decl, cxcursor::getCursorTU(cur)); // 
cursor with FormatAttr
+  return clang_getNullCursor();
+}
+
+CXString clang_FormatAttr_getType(CXCursor cur) {
+  const FormatAttr *fa = 
dyn_cast_or_null(cxcursor::getCursorAttr(cur));
+  if (!fa) return cxstring::createEmpty();
+  return cxstring::createDup(fa->getType()->getName());
+}
+
+unsigned clang_FormatAttr_getFormatIdx(CXCursor cur) {
+  const FormatAttr *fa = 
dyn_cast_or_null(cxcursor::getCursorAttr(cur));
+  if (!fa) return 0;
+  return fa->getFormatIdx();
+}
+
+unsigned clang_FormatAttr_getFirstArg(CXCursor cur) {
+  const FormatAttr *fa = 
dyn_cast_or_null(cxcursor::getCursorAttr(cur));
+  if (!fa) return 0;
+  return fa->getFirstArg();
+}
+
 int clang_Cursor_getNumArguments(CXCursor C) {
   if (clang_isDeclaration(C.kind)) {
 const Decl *D = cxcursor::getCursorDecl(C);
diff --git a/clang/tools/libclang/libclang.map 
b/clang/tools/libclang/libclang.map
index 25d8ba57d32514..7737abe3288de9 100644
--- a/clang/tools/libclang/libclang.map
+++ b/clang/tools/libclang/libclang.map
@@ -59,6 +59,10 @@ LLVM_13 {
 clang_Cursor_getMangling;
 clang_Cursor_getModule;
 clang_Cursor_getNumArguments;
+clang_Cursor_getFormatAttr;
+clang_FormatAttr_getType;
+clang_Format

[clang] [llvm] Expose C format (attribute) info for function declarations in Clang Index API (PR #113754)

2024-10-25 Thread AR Visions via cfe-commits

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


[clang] [llvm] Enable format info for C function declarations in Clang Index API, so we may have this context (PR #113754)

2024-10-25 Thread AR Visions via cfe-commits

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


[clang] [llvm] Expose C format (attribute) info for C function declarations in Clang Index API (PR #113754)

2024-10-25 Thread AR Visions via cfe-commits

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


[clang] [llvm] Expose format (attribute) info for function declarations in Clang Index API (PR #113754)

2024-10-25 Thread AR Visions via cfe-commits

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


[clang] [llvm] Expose format (attribute) info for function declarations in Clang Index API (PR #113754)

2024-10-25 Thread AR Visions via cfe-commits

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


[clang] [llvm] Expose format (attribute) info for function declarations in Clang Index API (PR #113754)

2024-10-25 Thread AR Visions via cfe-commits

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


[clang] [llvm] Expose format (attribute) info for function declarations in Clang Index API (PR #113754)

2024-10-25 Thread AR Visions via cfe-commits

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


[clang] [llvm] Expose format (attribute) info for function declarations in Clang Index API (PR #113754)

2024-11-08 Thread AR Visions via cfe-commits

ar-visions wrote:

I should note that this patch is useful for providing security context to 
parsers that use formatting.  Users that rely on Clang to provide info on the C 
functions REQUIRE this security validation context.  A parser that does no 
processing on this but relies on user hard coding of formatter-enabled 
functions from C will simply have more boilerplate and more security issues in 
general.

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