diff --git a/clang/test/ClangScanDeps/cl-output.c b/clang/test/ClangScanDeps/cl-output.c --- a/clang/test/ClangScanDeps/cl-output.c +++ b/clang/test/ClangScanDeps/cl-output.c @@ -46,6 +46,10 @@ "file": "DIR/test.c", "directory": "DIR", "command": "clang-cl /c -o DIR/test.o -o DIR/last.o -- DIR/test.c" +},{ + "file": "DIR/test.c", + "directory": "DIR", + "command": "clang-cl /c /o /opt/test.o -- DIR/test.c" }] //--- test.c @@ -81,7 +85,9 @@ // Check that the last argument specifying the output path wins. // // RUN: sed -e "s|DIR|%/t|g" %t/last-arg-cdb.json.template > %t/last-arg-cdb.json -// RUN: clang-scan-deps -compilation-database %t/last-arg-cdb.json > %t/last-arg-result.d +// RUN: clang-scan-deps -compilation-database %t/last-arg-cdb.json -j 1 > %t/last-arg-result.d // RUN: cat %t/last-arg-result.d | sed 's:\\\\\?:/:g' | FileCheck %s -DPREFIX=%/t --check-prefix=CHECK-LAST // CHECK-LAST: [[PREFIX]]/last.o: // CHECK-LAST-NEXT: [[PREFIX]]/test.c +// CHECK-LAST-NEXT: /opt/test.o: +// CHECK-LAST-NEXT: [[PREFIX]]/test.c diff --git a/clang/tools/clang-scan-deps/ClangScanDeps.cpp b/clang/tools/clang-scan-deps/ClangScanDeps.cpp --- a/clang/tools/clang-scan-deps/ClangScanDeps.cpp +++ b/clang/tools/clang-scan-deps/ClangScanDeps.cpp @@ -470,27 +470,29 @@ llvm::sys::path::stem(Args[0]).contains_insensitive("clang-cl") || llvm::is_contained(Args, "--driver-mode=cl"); - // Reverse scan, starting at the end or at the element before "--". - auto R = std::make_reverse_iterator(FlagsEnd); - for (auto I = R, E = Args.rend(); I != E; ++I) { + for (auto I = Args.begin(); I != FlagsEnd; ++I) { StringRef Arg = *I; if (ClangCLMode) { // Ignore arguments that are preceded by "-Xclang". - if ((I + 1) != E && I[1] == "-Xclang") - continue; - if (LastO.empty()) { - // With clang-cl, the output obj file can be specified with - // "/opath", "/o path", "/Fopath", and the dash counterparts. - // Also, clang-cl adds ".obj" extension if none is found. - if ((Arg == "-o" || Arg == "/o") && I != R) - LastO = I[-1]; // Next argument (reverse iterator) - else if (Arg.startswith("/Fo") || Arg.startswith("-Fo")) - LastO = Arg.drop_front(3).str(); - else if (Arg.startswith("/o") || Arg.startswith("-o")) - LastO = Arg.drop_front(2).str(); - - if (!LastO.empty() && !llvm::sys::path::has_extension(LastO)) - LastO.append(".obj"); + if (Arg == "-Xclang") + ++I; + + // With clang-cl, the output obj file can be specified with + // "/opath", "/o path", "/Fopath", and the dash counterparts. + // Also, clang-cl adds ".obj" extension if none is found. + llvm::StringRef CurrentO; + if ((Arg == "/o" || Arg == "-o") && I != FlagsEnd) + CurrentO = *++I; + else if (Arg.startswith("/Fo") || Arg.startswith("-Fo")) + CurrentO = Arg.drop_front(3); + else if (Arg.startswith("/o") || Arg.startswith("-o")) + CurrentO = Arg.drop_front(2); + + if (!CurrentO.empty()) { + if (!llvm::sys::path::has_extension(CurrentO)) + LastO = (CurrentO + ".obj").str(); + else + LastO = CurrentO.str(); } } if (Arg == "-resource-dir")