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 0e7c6c7900133..61de1404c127d 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 0c5ac80772e2b..d27c571b77640 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 562228cc334f3..1ec0dc0512319 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 25d8ba57d3251..7737abe3288de 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_FormatAttr_getFormatIdx; + clang_FormatAttr_getFirstArg; clang_Cursor_getNumTemplateArguments; clang_Cursor_getObjCDeclQualifiers; clang_Cursor_getObjCManglings;