aboutsummaryrefslogtreecommitdiff
path: root/compiler_wrapper/clang_syntax_flag_test.go
diff options
context:
space:
mode:
authorTobias Bosch <tbosch@google.com>2019-07-08 10:59:14 -0700
committerTobias Bosch <tbosch@google.com>2019-07-09 08:19:51 +0000
commitd868417979bda2479cf8ce0e01768585db2e1f5c (patch)
tree78399156fbb3e8df4c7bd75933ba5b41f650b009 /compiler_wrapper/clang_syntax_flag_test.go
parent7727c9fe68894c37ab14467f4d79cfc0934227ef (diff)
downloadtoolchain-utils-d868417979bda2479cf8ce0e01768585db2e1f5c.tar.gz
Support -clang-syntax flag in the compiler wrapper.
BUG=chromium:773875 TEST=unit test Change-Id: Iea4a3c395c09b2a97d8a9975cf642f985f2dbec7 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/toolchain-utils/+/1691113 Reviewed-by: George Burgess <gbiv@chromium.org> Tested-by: Tobias Bosch <tbosch@google.com>
Diffstat (limited to 'compiler_wrapper/clang_syntax_flag_test.go')
-rw-r--r--compiler_wrapper/clang_syntax_flag_test.go133
1 files changed, 133 insertions, 0 deletions
diff --git a/compiler_wrapper/clang_syntax_flag_test.go b/compiler_wrapper/clang_syntax_flag_test.go
new file mode 100644
index 00000000..4f0a077b
--- /dev/null
+++ b/compiler_wrapper/clang_syntax_flag_test.go
@@ -0,0 +1,133 @@
+package main
+
+import (
+ "errors"
+ "fmt"
+ "io"
+ "path"
+ "strings"
+ "testing"
+)
+
+func TestCheckClangSyntaxByNestedCall(t *testing.T) {
+ withTestContext(t, func(ctx *testContext) {
+ ctx.cmdMock = func(cmd *command, stdout io.Writer, stderr io.Writer) error {
+ if ctx.cmdCount == 1 {
+ if err := verifyPath(cmd, "usr/bin/clang"); err != nil {
+ return err
+ }
+ if err := verifyArgOrder(cmd, mainCc, "-fsyntax-only", `-stdlib=libstdc\+\+`); err != nil {
+ return err
+ }
+ }
+ return nil
+ }
+ cmd := ctx.must(callCompiler(ctx, ctx.cfg,
+ ctx.newCommand(gccX86_64, "-clang-syntax", mainCc)))
+ if ctx.cmdCount != 2 {
+ t.Errorf("expected 2 calls. Got: %d", ctx.cmdCount)
+ }
+ if err := verifyPath(cmd, gccX86_64+".real"); err != nil {
+ t.Error(err)
+ }
+ if err := verifyArgCount(cmd, 0, "-clang-syntax"); err != nil {
+ t.Error(err)
+ }
+ })
+}
+
+func TestForwardStdOutAndStderrFromClangSyntaxCheck(t *testing.T) {
+ withTestContext(t, func(ctx *testContext) {
+ ctx.cmdMock = func(cmd *command, stdout io.Writer, stderr io.Writer) error {
+ if ctx.cmdCount == 1 {
+ fmt.Fprintf(stdout, "somemessage")
+ fmt.Fprintf(stderr, "someerror")
+ }
+ return nil
+ }
+ ctx.must(callCompiler(ctx, ctx.cfg,
+ ctx.newCommand(gccX86_64, "-clang-syntax", mainCc)))
+ if ctx.stdoutString() != "somemessage" {
+ t.Errorf("stdout was not forwarded. Got: %s", ctx.stdoutString())
+ }
+ if ctx.stderrString() != "someerror" {
+ t.Errorf("stderr was not forwarded. Got: %s", ctx.stderrString())
+ }
+ })
+}
+
+func TestForwardExitCodeFromClangSyntaxCheck(t *testing.T) {
+ withTestContext(t, func(ctx *testContext) {
+ ctx.cmdMock = func(cmd *command, stdout io.Writer, stderr io.Writer) error {
+ if ctx.cmdCount == 1 {
+ return newExitCodeError(23)
+ }
+ return nil
+ }
+ exitCode := callCompiler(ctx, ctx.cfg,
+ ctx.newCommand(gccX86_64, "-clang-syntax", mainCc))
+ if exitCode != 23 {
+ t.Errorf("unexpected exit code. Got: %d", exitCode)
+ }
+ })
+}
+
+func TestReportGeneralErrorsFromClangSyntaxCheck(t *testing.T) {
+ withTestContext(t, func(ctx *testContext) {
+ ctx.cmdMock = func(cmd *command, stdout io.Writer, stderr io.Writer) error {
+ if ctx.cmdCount == 1 {
+ return errors.New("someerror")
+ }
+ return nil
+ }
+ stderr := ctx.mustFail(callCompiler(ctx, ctx.cfg,
+ ctx.newCommand(gccX86_64, "-clang-syntax", mainCc)))
+ if err := verifyInternalError(stderr); err != nil {
+ t.Fatal(err)
+ }
+ if !strings.Contains(stderr, "someerror") {
+ t.Errorf("unexpected error. Got: %s", stderr)
+ }
+ })
+}
+
+func TestIgnoreClangSyntaxCheckWhenCallingClang(t *testing.T) {
+ withTestContext(t, func(ctx *testContext) {
+ ctx.cmdMock = func(cmd *command, stdout io.Writer, stderr io.Writer) error {
+ if ctx.cmdCount > 1 {
+ return fmt.Errorf("Unexpected call %#v", cmd)
+ }
+ return nil
+ }
+ cmd := ctx.must(callCompiler(ctx, ctx.cfg,
+ ctx.newCommand(clangX86_64, "-clang-syntax", mainCc)))
+ if err := verifyArgCount(cmd, 0, "-clang-syntax"); err != nil {
+ t.Error(err)
+ }
+ })
+}
+
+func TestUseGomaForClangSyntaxCheck(t *testing.T) {
+ withTestContext(t, func(ctx *testContext) {
+ gomaPath := path.Join(ctx.tempDir, "gomacc")
+ // Create a file so the gomacc path is valid.
+ ctx.writeFile(gomaPath, "")
+ ctx.env = []string{"GOMACC_PATH=" + gomaPath}
+ ctx.cmdMock = func(cmd *command, stdout io.Writer, stderr io.Writer) error {
+ if ctx.cmdCount == 1 {
+ if err := verifyPath(cmd, gomaPath); err != nil {
+ return err
+ }
+ if err := verifyArgOrder(cmd, "usr/bin/clang", mainCc); err != nil {
+ return err
+ }
+ }
+ return nil
+ }
+ ctx.must(callCompiler(ctx, ctx.cfg,
+ ctx.newCommand(gccX86_64, "-clang-syntax", mainCc)))
+ if ctx.cmdCount != 2 {
+ t.Errorf("expected 2 calls. Got: %d", ctx.cmdCount)
+ }
+ })
+}