93 lines · plain
1# RUN: rm -rf %t.dir && mkdir -p %t.dir2 3# The mock driver below is a shell script:4# REQUIRES: shell5 6# Create a bin directory to store the mock-driver and add it to the path7# RUN: mkdir -p %t.dir/bin8# RUN: %python -c "print(__import__('os').environ['PATH'])" > %t.path9# RUN: export PATH=%t.dir/bin:%{readfile:%t.path}10# Generate a mock-driver that will print %temp_dir%/my/dir and11# %temp_dir%/my/dir2 as include search paths.12# RUN: echo '#!/bin/sh' >> %t.dir/bin/my_driver.sh13# RUN: echo '[ "$0" = "%t.dir/bin/my_driver.sh" ] || exit' >> %t.dir/bin/my_driver.sh14# RUN: echo '[ "$1" = "-print-file-name=include" ] && echo "%t.dir/builtin" && exit' >> %t.dir/bin/my_driver.sh15# RUN: echo 'args="$*"' >> %t.dir/bin/my_driver.sh16# Check that clangd preserves certain flags like `-nostdinc` from17# original invocation in compile_commands.json.18# RUN: echo '[ -z "${args##*"-nostdinc"*}" ] || exit' >> %t.dir/bin/my_driver.sh19# RUN: echo '[ -z "${args##*"--sysroot /my/sysroot/path"*}" ] || exit' >> %t.dir/bin/my_driver.sh20# RUN: echo '[ -z "${args##*"-isysroot /isysroot"*}" ] || exit' >> %t.dir/bin/my_driver.sh21# RUN: echo '[ -z "${args##*"-target arm-linux-gnueabihf"*}" ] || exit' >> %t.dir/bin/my_driver.sh22# RUN: echo '[ -z "${args##*"--stdlib libc++"*}" ] || exit' >> %t.dir/bin/my_driver.sh23# RUN: echo '[ -z "${args##*"-specs=test.spec"*}" ] || exit' >> %t.dir/bin/my_driver.sh24# RUN: echo '[ -z "${args##*"--specs=test2.spec"*}" ] || exit' >> %t.dir/bin/my_driver.sh25# Check that clangd drops other flags like -lc++, which don't affect includes26# RUN: echo '[ -n "${args##*"-lc++"*}" ] || exit' >> %t.dir/bin/my_driver.sh27# RUN: echo 'echo line to ignore >&2' >> %t.dir/bin/my_driver.sh28# RUN: echo 'printf "Target: arm-linux-gnueabihf\r\n" >&2' >> %t.dir/bin/my_driver.sh29# RUN: echo 'printf "#include <...> search starts here:\r\n" >&2' >> %t.dir/bin/my_driver.sh30# RUN: echo 'echo %t.dir/my/dir/ >&2' >> %t.dir/bin/my_driver.sh31# RUN: echo 'echo %t.dir/my/dir2/ >&2' >> %t.dir/bin/my_driver.sh32# RUN: echo 'echo %t.dir/builtin >&2' >> %t.dir/bin/my_driver.sh33# RUN: echo 'printf "End of search list.\r\n" >&2' >> %t.dir/bin/my_driver.sh34# RUN: chmod +x %t.dir/bin/my_driver.sh35 36# Create header files my/dir/a.h and my/dir2/b.h37# RUN: mkdir -p %t.dir/my/dir38# RUN: touch %t.dir/my/dir/a.h39# RUN: mkdir -p %t.dir/my/dir240# RUN: touch %t.dir/my/dir2/b.h41# RUN: mkdir -p %t.dir/builtin42# RUN: touch %t.dir/builtin/c.h43 44# Generate a compile_commands.json that will query the mock driver we've45# created. Which should add a.h and b.h into include search path.46# RUN: echo '[{"directory": "%/t.dir", "command": "my_driver.sh the-file.cpp --target=arm-linux-gnueabihf -nostdinc --sysroot /my/sysroot/path -isysroot/isysroot -stdlib=libc++ -lc++ -specs=test.spec --specs=test2.spec", "file": "the-file.cpp"}]' > %t.dir/compile_commands.json47 48# RUN: sed -e "s|INPUT_DIR|%/t.dir|g" %s > %t.test.149# On Windows, we need the URI in didOpen to look like "uri":"file:///C:/..."50# (with the extra slash in the front), so we add it here.51# RUN: sed -E -e 's|"file://([A-Z]):/|"file:///\1:/|g' %t.test.1 > %t.test52 53# Bless the mock driver we've just created so that clangd can execute it.54# Note: include clangd's stderr in the FileCheck input with "2>&1" so that we55# can match output lines like "ASTWorker building file"56# RUN: clangd --clang-tidy=false -lit-test -query-driver="**.test,**.sh" < %t.test 2>&1 | FileCheck -strict-whitespace %t.test57{"jsonrpc":"2.0","id":0,"method":"initialize","params":{}}58---59{60 "jsonrpc":"2.0",61 "method":"textDocument/didOpen",62 "params": {63 "textDocument": {64 "uri": "file://INPUT_DIR/the-file.cpp",65 "languageId":"cpp",66 "version":1,67 "text":"#include <a.h>\n#include <b.h>\n#if !defined(__ARM_ARCH) || !defined(__gnu_linux__)\n#error \"Invalid target\"\n#endif\n#if __has_include(<c.h>)\n#error \"wrong-toolchain builtins\"\n#endif\n"68 }69 }70}71# Look for the "ASTWorker building file" line so that the subsequent diagnostics72# that are matches are for the C++ source file and not a config file.73# CHECK: ASTWorker building file74# CHECK: "method": "textDocument/publishDiagnostics",75# CHECK-NEXT: "params": {76# CHECK-NEXT: "diagnostics": [],77# CHECK-NEXT: "uri": "file://{{.*}}/the-file.cpp",78---79{"jsonrpc":"2.0","id":10000,"method":"shutdown"}80---81{"jsonrpc":"2.0","method":"exit"}82 83# Generate a different compile_commands.json which does not point to the mock driver84# RUN: echo '[{"directory": "%/t.dir", "command": "gcc the-file.cpp --target=arm-linux-gnueabihf -nostdinc --sysroot /my/sysroot/path -isysroot/isysroot -stdlib=libc++ -lc++ -specs=test.spec --specs=test2.spec", "file": "the-file.cpp"}]' > %t.dir/compile_commands.json85 86# Generate a clangd config file which points to the mock driver instead87# RUN: echo 'CompileFlags:' > %t.dir/.clangd88# RUN: echo ' Compiler: my_driver.sh' >> %t.dir/.clangd89 90# Run clangd a second time, to make sure it picks up the driver name from the config file91# Note, we need to pass -enable-config because -lit-test otherwise disables it92# RUN: clangd --clang-tidy=false -lit-test -enable-config -query-driver="**.test,**.sh" < %t.test 2>&1 | FileCheck -strict-whitespace %t.test93