@@ -2,11 +2,16 @@ 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+ LLGoFiles = "$(llvm-config --cflags): _wrap/clang.cpp"
14+ LLGoPackage = "link: -L$(llvm-config --libdir) -lclang; -lclang"
1015)
1116
1217type Config struct {
@@ -26,8 +31,14 @@ const TEMP_FILE = "temp.h"
2631func CreateTranslationUnit (config * Config ) (* clang.Index , * clang.TranslationUnit , error ) {
2732 // default use the c/c++ standard of clang; c:gnu17 c++:gnu++17
2833 // https://clang.llvm.org/docs/CommandGuide/clang.html
29- allArgs := clangtool .WithSysRoot (append (defaultArgs (config .IsCpp ), config .Args ... ))
3034
35+ executableName := "clang"
36+ path , err := exec .LookPath (executableName )
37+ if err != nil {
38+ return nil , nil , err
39+ }
40+
41+ allArgs := append (append ([]string {path }, defaultArgs (config .IsCpp )... ), config .Args ... )
3142 cArgs := make ([]* c.Char , len (allArgs ))
3243 for i , arg := range allArgs {
3344 cArgs [i ] = c .AllocaCStr (arg )
@@ -49,22 +60,25 @@ func CreateTranslationUnit(config *Config) (*clang.Index, *clang.TranslationUnit
4960 Contents : content ,
5061 Length : c .Ulong (c .Strlen (content )),
5162 }
52-
53- unit = index .ParseTranslationUnit (
63+ code := ParseTranslationUnit2FullArgv (index ,
5464 tempFile .Filename ,
5565 unsafe .SliceData (cArgs ), c .Int (len (cArgs )),
5666 tempFile , 1 ,
5767 clang .DetailedPreprocessingRecord ,
68+ & unit ,
5869 )
59-
70+ c . Printf ( c . Str ( "code: %d \n " ), code )
6071 } else {
72+
6173 cFile := c .AllocaCStr (config .File )
62- unit = index . ParseTranslationUnit (
74+ code := ParseTranslationUnit2FullArgv ( index ,
6375 cFile ,
6476 unsafe .SliceData (cArgs ), c .Int (len (cArgs )),
6577 nil , 0 ,
6678 clang .DetailedPreprocessingRecord ,
79+ & unit ,
6780 )
81+ c .Printf (c .Str ("code: %d\n " ), code )
6882 }
6983
7084 if unit == nil {
@@ -114,3 +128,43 @@ func defaultArgs(isCpp bool) []string {
114128 }
115129 return args
116130}
131+
132+ // CINDEX_LINKAGE CXTranslationUnit clang_parseTranslationUnit(CXIndex CIdx, const char *source_filename,
133+ // const char *const *command_line_args,
134+ // int num_command_line_args,
135+ // struct CXUnsavedFile *unsaved_files,
136+ // unsigned num_unsaved_files, unsigned options);
137+
138+ /**
139+ * Same as \c clang_parseTranslationUnit2, but returns
140+ * the \c CXTranslationUnit instead of an error code. In case of an error this
141+ * routine returns a \c NULL \c CXTranslationUnit, without further detailed
142+ * error codes.
143+ */
144+ //go:linkname ParseTranslationUnit C.clang_parseTranslationUnit
145+ func ParseTranslationUnit (index * clang.Index , sourceFilename * c.Char , commandLineArgs * * c.Char , numCommandLineArgs c.Int ,
146+ unsavedFiles * clang.UnsavedFile , numUnsavedFiles c.Uint , options c.Uint ) * clang.TranslationUnit
147+
148+ /**
149+ * Same as clang_parseTranslationUnit2 but requires a full command line
150+ * for \c command_line_args including argv[0]. This is useful if the standard
151+ * library paths are relative to the binary.
152+ */
153+ // CINDEX_LINKAGE enum CXErrorCode
154+ // clang_parseTranslationUnit2FullArgv(CXIndex CIdx, const char *source_filename, const char *const *command_line_args,
155+ // int num_command_line_args, struct CXUnsavedFile *unsaved_files,
156+ // unsigned num_unsaved_files, unsigned options, CXTranslationUnit *out_TU);
157+
158+ type ErrorCode int
159+
160+ const (
161+ Error_Success = 0
162+ Error_Failure = 1
163+ Error_Crashed = 2
164+ Error_InvalidArguments = 3
165+ Error_ASTReadError = 4
166+ )
167+
168+ //go:linkname ParseTranslationUnit2FullArgv C.clang_parseTranslationUnit2FullArgv
169+ func ParseTranslationUnit2FullArgv (index * clang.Index , sourceFilename * c.Char , commandLineArgs * * c.Char , numCommandLineArgs c.Int ,
170+ unsavedFiles * clang.UnsavedFile , numUnsavedFiles c.Uint , options c.Uint , out_TU * * clang.TranslationUnit ) ErrorCode
0 commit comments