예제 #1
0
func TestCompilationDatabase(t *testing.T) {
	db, err := clang.NewCompilationDatabase("testdata")
	if err != nil {
		t.Fatalf("error loading compilation database: %v", err)
	}
	defer db.Dispose()

	table := []struct {
		directory string
		args      []string
	}{
		{
			directory: "/home/user/llvm/build",
			args: []string{
				"/usr/bin/clang++",
				"-Irelative",
				//FIXME: bug in clang ?
				//`-DSOMEDEF="With spaces, quotes and \-es.`,
				"-DSOMEDEF=With spaces, quotes and -es.",
				"-c",
				"-o",
				"file.o",
				"file.cc",
			},
		},
		{
			directory: "@TESTDIR@",
			args:      []string{"g++", "-c", "-DMYMACRO=a", "subdir/a.cpp"},
		},
	}

	cmds := db.GetAllCompileCommands()
	if cmds.GetSize() != len(table) {
		t.Errorf("expected #cmds=%d. got=%d", len(table), cmds.GetSize())
	}

	for i := 0; i < cmds.GetSize(); i++ {
		cmd := cmds.GetCommand(i)
		if cmd.GetDirectory() != table[i].directory {
			t.Errorf("expected dir=%q. got=%q", table[i].directory, cmd.GetDirectory())
		}

		nargs := cmd.GetNumArgs()
		if nargs != len(table[i].args) {
			t.Errorf("expected #args=%d. got=%d", len(table[i].args), nargs)
		}
		if nargs > len(table[i].args) {
			nargs = len(table[i].args)
		}
		for j := 0; j < nargs; j++ {
			arg := cmd.GetArg(j)
			if arg != table[i].args[j] {
				t.Errorf("expected arg[%d]=%q. got=%q", j, table[i].args[j], arg)
			}
		}
	}
}
예제 #2
0
func TestCompilationDatabaseError(t *testing.T) {
	_, err := clang.NewCompilationDatabase("testdata-not-there")
	if err == nil {
		t.Fatalf("expected an error")
	}

	if err.(clang.CompilationDatabaseError) != clang.CompilationDatabase_CanNotLoadDatabase {
		t.Fatalf("expected %v", clang.CompilationDatabase_CanNotLoadDatabase)
	}
}
예제 #3
0
파일: main.go 프로젝트: zimmski/go-clang
func main() {
	if len(os.Args) <= 1 {
		fmt.Printf("**error: you need to give a directory containing a 'compile_commands.json' file\n")
		os.Exit(1)
	}
	dir := os.ExpandEnv(os.Args[1])
	fmt.Printf(":: inspecting [%s]...\n", dir)

	fname := filepath.Join(dir, "compile_commands.json")
	f, err := os.Open(fname)
	if err != nil {
		fmt.Printf("**error: could not open file [%s]: %v\n", fname, err)
		os.Exit(1)
	}
	f.Close()

	db, err := clang.NewCompilationDatabase(dir)
	if err != nil {
		fmt.Printf("**error: could not open compilation database at [%s]: %v\n", dir, err)
		os.Exit(1)
	}
	defer db.Dispose()

	cmds := db.GetAllCompileCommands()
	ncmds := cmds.GetSize()
	fmt.Printf(":: got %d compile commands\n", ncmds)
	for i := 0; i < ncmds; i++ {
		cmd := cmds.GetCommand(i)
		fmt.Printf("::  --- cmd=%d ---\n", i)
		fmt.Printf("::  dir= %q\n", cmd.GetDirectory())
		nargs := cmd.GetNumArgs()
		fmt.Printf("::  nargs= %d\n", nargs)
		sargs := make([]string, 0, nargs)
		for iarg := 0; iarg < nargs; iarg++ {
			arg := cmd.GetArg(iarg)
			sfmt := "%q, "
			if iarg+1 == nargs {
				sfmt = "%q"
			}
			sargs = append(sargs, fmt.Sprintf(sfmt, arg))

		}
		fmt.Printf("::  args= {%s}\n", strings.Join(sargs, ""))
		if i+1 != ncmds {
			fmt.Printf("::\n")
		}
	}
	fmt.Printf(":: inspecting [%s]... [done]\n", dir)
}