Skip to content

Expose format (attribute) info for function declarations in Clang Index API #113754

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,5 @@ pythonenv*
/clang/utils/analyzer/projects/*/RefScanBuildResults
# automodapi puts generated documentation files here.
/lldb/docs/python_api/
silver-debug
silver-build
Copy link
Contributor

Choose a reason for hiding this comment

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

Those are unrelated changes that should remain local to your development environment

23 changes: 23 additions & 0 deletions clang/include/clang-c/Index.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

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

No need to align the indentation of the arguments here (same below)


/**
* 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.
*
Expand Down
26 changes: 26 additions & 0 deletions clang/tools/libclang/CXCursor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<FunctionDecl>(decl))
if (const FormatAttr *fa = fd->getAttr<FormatAttr>())
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<FormatAttr>(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<FormatAttr>(cxcursor::getCursorAttr(cur));
if (!fa) return 0;
return fa->getFormatIdx();
}

unsigned clang_FormatAttr_getFirstArg(CXCursor cur) {
const FormatAttr *fa = dyn_cast_or_null<FormatAttr>(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);
Expand Down
4 changes: 4 additions & 0 deletions clang/tools/libclang/libclang.map
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Comment on lines +62 to +65
Copy link
Contributor

Choose a reason for hiding this comment

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

These should be inserted under the header for the next version to be released, i.e. LLVM_20

clang_Cursor_getNumTemplateArguments;
clang_Cursor_getObjCDeclQualifiers;
clang_Cursor_getObjCManglings;
Expand Down
Loading