Skip to content

Commit

Permalink
Merge pull request #979 from luoliwoshang/clang/presume
Browse files Browse the repository at this point in the history
c/clang:presume location
  • Loading branch information
xushiwei authored Feb 6, 2025
2 parents 29ec301 + 0a0bb12 commit a345746
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 6 deletions.
21 changes: 15 additions & 6 deletions c/clang/_demo/symboldump/symboldump.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,16 @@ var context = newContext()
func printCursorLocation(cursor clang.Cursor) {
loc := cursor.Location()
var file clang.File
var line, column c.Uint
var line, column, presumedLine, presumedColumn c.Uint
var presumedFilename clang.String

loc.SpellingLocation(&file, &line, &column, nil)
loc.PresumedLocation(&presumedFilename, &presumedLine, &presumedColumn)
filename := file.FileName()
defer filename.Dispose()

c.Printf(c.Str("%s:%d:%d\n"), filename.CStr(), line, column)
c.Printf(c.Str("Location: %s:%d:%d\n"), filename.CStr(), line, column)
c.Printf(c.Str("Presumed Location: %s:%d:%d\n"), presumedFilename.CStr(), presumedLine, presumedColumn)
}

func printMarcoInfo(cursor clang.Cursor) {
Expand Down Expand Up @@ -107,20 +110,26 @@ func printFuncInfo(cursor clang.Cursor) {
}

func visit(cursor, parent clang.Cursor, clientData c.Pointer) clang.ChildVisitResult {
if cursor.Kind == clang.CursorMacroDefinition {
switch cursor.Kind {
case clang.CursorMacroDefinition:
printMarcoInfo(cursor)
} else if cursor.Kind == clang.CursorNamespace {
case clang.CursorNamespace:
nameStr := cursor.String()
context.setNamespaceName(c.GoString(nameStr.CStr()))
clang.VisitChildren(cursor, visit, nil)
context.setNamespaceName("")
} else if cursor.Kind == clang.CursorClassDecl {
case clang.CursorClassDecl:
nameStr := cursor.String()
context.setClassName(c.GoString(nameStr.CStr()))
clang.VisitChildren(cursor, visit, nil)
context.setClassName("")
} else if cursor.Kind == clang.CursorCXXMethod || cursor.Kind == clang.CursorFunctionDecl {
case clang.CursorCXXMethod, clang.CursorFunctionDecl:
printFuncInfo(cursor)
case clang.CursorEnumDecl, clang.CursorStructDecl, clang.CursorUnionDecl, clang.CursorTypedefDecl:
nameStr := cursor.String()
printCursorLocation(cursor)
c.Printf(c.Str("Name: %s\n"), nameStr.CStr())
println("--------------------------------")
}

return clang.ChildVisit_Continue
Expand Down
4 changes: 4 additions & 0 deletions c/clang/_wrap/cursor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,10 @@ void wrap_clang_getSpellingLocation(CXSourceLocation *loc, CXFile *file, unsigne
clang_getSpellingLocation(*loc, file, line, column, offset);
}

void wrap_clang_getPresumedLocation(CXSourceLocation *loc, CXString *filename, unsigned *line, unsigned *column) {
clang_getPresumedLocation(*loc, filename, line, column);
}

void wrap_clang_getRangeStart(CXSourceRange *range, CXSourceLocation *loc) { *loc = clang_getRangeStart(*range); }

void wrap_clang_getRangeEnd(CXSourceRange *range, CXSourceLocation *loc) { *loc = clang_getRangeEnd(*range); }
Expand Down
47 changes: 47 additions & 0 deletions c/clang/clang.go
Original file line number Diff line number Diff line change
Expand Up @@ -2878,6 +2878,53 @@ func (l SourceLocation) Offset() (ret c.Uint) {
return
}

/**
* Retrieve the file, line and column represented by the given source
* location, as specified in a # line directive.
*
* Example: given the following source code in a file somefile.c
*
* \code
* #123 "dummy.c" 1
*
* static int func(void)
* {
* return 0;
* }
* \endcode
*
* the location information returned by this function would be
*
* File: dummy.c Line: 124 Column: 12
*
* whereas clang_getExpansionLocation would have returned
*
* File: somefile.c Line: 3 Column: 12
*
* \param location the location within a source file that will be decomposed
* into its parts.
*
* \param filename [out] if non-NULL, will be set to the filename of the
* source location. Note that filenames returned will be for "virtual" files,
* which don't necessarily exist on the machine running clang - e.g. when
* parsing preprocessed output obtained from a different environment. If
* a non-NULL value is passed in, remember to dispose of the returned value
* using \c clang_disposeString() once you've finished with it. For an invalid
* source location, an empty string is returned.
*
* \param line [out] if non-NULL, will be set to the line number of the
* source location. For an invalid source location, zero is returned.
*
* \param column [out] if non-NULL, will be set to the column number of the
* source location. For an invalid source location, zero is returned.
*/
// llgo:link (*SourceLocation).wrapPresumedLocation C.wrap_clang_getPresumedLocation
func (l *SourceLocation) wrapPresumedLocation(filename *String, line, column *c.Uint) {}

func (l SourceLocation) PresumedLocation(filename *String, line, column *c.Uint) {
l.wrapPresumedLocation(filename, line, column)
}

/**
* Retrieve a source location representing the first character within a
* source range.
Expand Down

0 comments on commit a345746

Please sign in to comment.