@@ -2,11 +2,15 @@ package clang
22
33import (
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
1216type Config struct {
@@ -26,8 +30,14 @@ const TEMP_FILE = "temp.h"
2630func 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 )
@@ -41,32 +51,38 @@ func CreateTranslationUnit(config *Config) (*clang.Index, *clang.TranslationUnit
4151 }
4252
4353 var unit * clang.TranslationUnit
44-
54+ var code ErrorCode
4555 if config .Temp {
4656 content := c .AllocaCStr (config .File )
4757 tempFile := & clang.UnsavedFile {
4858 Filename : c .Str (TEMP_FILE ),
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-
6069 } else {
70+
6171 cFile := c .AllocaCStr (config .File )
62- unit = index . ParseTranslationUnit (
72+ code = ParseTranslationUnit2FullArgv ( index ,
6373 cFile ,
6474 unsafe .SliceData (cArgs ), c .Int (len (cArgs )),
6575 nil , 0 ,
6676 clang .DetailedPreprocessingRecord ,
77+ & unit ,
6778 )
6879 }
6980
81+ if code != Error_Success {
82+ c .Printf (c .Str ("code: %d\n " ), code )
83+ return nil , nil , errors .New ("failed to parse translation unit" )
84+ }
85+
7086 if unit == nil {
7187 return nil , nil , errors .New ("failed to parse translation unit" )
7288 }
@@ -114,3 +130,43 @@ func defaultArgs(isCpp bool) []string {
114130 }
115131 return args
116132}
133+
134+ // CINDEX_LINKAGE CXTranslationUnit clang_parseTranslationUnit(CXIndex CIdx, const char *source_filename,
135+ // const char *const *command_line_args,
136+ // int num_command_line_args,
137+ // struct CXUnsavedFile *unsaved_files,
138+ // unsigned num_unsaved_files, unsigned options);
139+
140+ /**
141+ * Same as \c clang_parseTranslationUnit2, but returns
142+ * the \c CXTranslationUnit instead of an error code. In case of an error this
143+ * routine returns a \c NULL \c CXTranslationUnit, without further detailed
144+ * error codes.
145+ */
146+ //go:linkname ParseTranslationUnit C.clang_parseTranslationUnit
147+ func ParseTranslationUnit (index * clang.Index , sourceFilename * c.Char , commandLineArgs * * c.Char , numCommandLineArgs c.Int ,
148+ unsavedFiles * clang.UnsavedFile , numUnsavedFiles c.Uint , options c.Uint ) * clang.TranslationUnit
149+
150+ /**
151+ * Same as clang_parseTranslationUnit2 but requires a full command line
152+ * for \c command_line_args including argv[0]. This is useful if the standard
153+ * library paths are relative to the binary.
154+ */
155+ // CINDEX_LINKAGE enum CXErrorCode
156+ // clang_parseTranslationUnit2FullArgv(CXIndex CIdx, const char *source_filename, const char *const *command_line_args,
157+ // int num_command_line_args, struct CXUnsavedFile *unsaved_files,
158+ // unsigned num_unsaved_files, unsigned options, CXTranslationUnit *out_TU);
159+
160+ type ErrorCode int
161+
162+ const (
163+ Error_Success = 0
164+ Error_Failure = 1
165+ Error_Crashed = 2
166+ Error_InvalidArguments = 3
167+ Error_ASTReadError = 4
168+ )
169+
170+ //go:linkname ParseTranslationUnit2FullArgv C.clang_parseTranslationUnit2FullArgv
171+ func ParseTranslationUnit2FullArgv (index * clang.Index , sourceFilename * c.Char , commandLineArgs * * c.Char , numCommandLineArgs c.Int ,
172+ unsavedFiles * clang.UnsavedFile , numUnsavedFiles c.Uint , options c.Uint , out_TU * * clang.TranslationUnit ) ErrorCode
0 commit comments