Skip to content

Commit 9d17cff

Browse files
committed
clang:full ParseTranslationUnit2FullArgv
1 parent 2f50921 commit 9d17cff

1 file changed

Lines changed: 59 additions & 6 deletions

File tree

_xtool/internal/clang/clang.go

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@ package clang
22

33
import (
44
"errors"
5+
"os/exec"
56
"unsafe"
67

78
"github.com/goplus/lib/c"
89
"github.com/goplus/lib/c/clang"
9-
"github.com/goplus/llcppg/_xtool/internal/clangtool"
10+
)
11+
12+
const (
13+
LLGoPackage = "link: -L$(llvm-config --libdir) -lclang; -lclang"
1014
)
1115

1216
type Config struct {
@@ -26,8 +30,14 @@ const TEMP_FILE = "temp.h"
2630
func CreateTranslationUnit(config *Config) (*clang.Index, *clang.TranslationUnit, error) {
2731
// default use the c/c++ standard of clang; c:gnu17 c++:gnu++17
2832
// https://clang.llvm.org/docs/CommandGuide/clang.html
29-
allArgs := clangtool.WithSysRoot(append(defaultArgs(config.IsCpp), config.Args...))
3033

34+
executableName := "clang"
35+
path, err := exec.LookPath(executableName)
36+
if err != nil {
37+
return nil, nil, err
38+
}
39+
40+
allArgs := append(append([]string{path}, defaultArgs(config.IsCpp)...), config.Args...)
3141
cArgs := make([]*c.Char, len(allArgs))
3242
for i, arg := range allArgs {
3343
cArgs[i] = c.AllocaCStr(arg)
@@ -49,22 +59,25 @@ func CreateTranslationUnit(config *Config) (*clang.Index, *clang.TranslationUnit
4959
Contents: content,
5060
Length: c.Ulong(c.Strlen(content)),
5161
}
52-
53-
unit = index.ParseTranslationUnit(
62+
code := ParseTranslationUnit2FullArgv(index,
5463
tempFile.Filename,
5564
unsafe.SliceData(cArgs), c.Int(len(cArgs)),
5665
tempFile, 1,
5766
clang.DetailedPreprocessingRecord,
67+
&unit,
5868
)
59-
69+
c.Printf(c.Str("code: %d\n"), code)
6070
} else {
71+
6172
cFile := c.AllocaCStr(config.File)
62-
unit = index.ParseTranslationUnit(
73+
code := ParseTranslationUnit2FullArgv(index,
6374
cFile,
6475
unsafe.SliceData(cArgs), c.Int(len(cArgs)),
6576
nil, 0,
6677
clang.DetailedPreprocessingRecord,
78+
&unit,
6779
)
80+
c.Printf(c.Str("code: %d\n"), code)
6881
}
6982

7083
if unit == nil {
@@ -114,3 +127,43 @@ func defaultArgs(isCpp bool) []string {
114127
}
115128
return args
116129
}
130+
131+
// CINDEX_LINKAGE CXTranslationUnit clang_parseTranslationUnit(CXIndex CIdx, const char *source_filename,
132+
// const char *const *command_line_args,
133+
// int num_command_line_args,
134+
// struct CXUnsavedFile *unsaved_files,
135+
// unsigned num_unsaved_files, unsigned options);
136+
137+
/**
138+
* Same as \c clang_parseTranslationUnit2, but returns
139+
* the \c CXTranslationUnit instead of an error code. In case of an error this
140+
* routine returns a \c NULL \c CXTranslationUnit, without further detailed
141+
* error codes.
142+
*/
143+
//go:linkname ParseTranslationUnit C.clang_parseTranslationUnit
144+
func ParseTranslationUnit(index *clang.Index, sourceFilename *c.Char, commandLineArgs **c.Char, numCommandLineArgs c.Int,
145+
unsavedFiles *clang.UnsavedFile, numUnsavedFiles c.Uint, options c.Uint) *clang.TranslationUnit
146+
147+
/**
148+
* Same as clang_parseTranslationUnit2 but requires a full command line
149+
* for \c command_line_args including argv[0]. This is useful if the standard
150+
* library paths are relative to the binary.
151+
*/
152+
// CINDEX_LINKAGE enum CXErrorCode
153+
// clang_parseTranslationUnit2FullArgv(CXIndex CIdx, const char *source_filename, const char *const *command_line_args,
154+
// int num_command_line_args, struct CXUnsavedFile *unsaved_files,
155+
// unsigned num_unsaved_files, unsigned options, CXTranslationUnit *out_TU);
156+
157+
type ErrorCode int
158+
159+
const (
160+
Error_Success = 0
161+
Error_Failure = 1
162+
Error_Crashed = 2
163+
Error_InvalidArguments = 3
164+
Error_ASTReadError = 4
165+
)
166+
167+
//go:linkname ParseTranslationUnit2FullArgv C.clang_parseTranslationUnit2FullArgv
168+
func ParseTranslationUnit2FullArgv(index *clang.Index, sourceFilename *c.Char, commandLineArgs **c.Char, numCommandLineArgs c.Int,
169+
unsavedFiles *clang.UnsavedFile, numUnsavedFiles c.Uint, options c.Uint, out_TU **clang.TranslationUnit) ErrorCode

0 commit comments

Comments
 (0)