[Lldb-commits] [PATCH] D95995: [lldb] Simplify the logic to detect compiler flag support

Jonas Devlieghere via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Wed Feb 3 19:17:05 PST 2021


JDevlieghere created this revision.
JDevlieghere added reviewers: aprantl, kastiglione, labath.
JDevlieghere requested review of this revision.

This patch effectively does these things:

- Centralize the logic to figure out if a compiler flag is supported.
- Stop sanity checking whether the compiler works at all. While useful, that's not the decorator's responsibility.
- Invoke the compiler with `xcrun` on Darwin so we know where to find the sysroot.

On my machine, the clang invocation couldn't find libSystem and therefore the decorator would fail the sanity check and it would always try to run the ASan/UBSan/TSan tests even though I wasn't building compiler-rt.


https://reviews.llvm.org/D95995

Files:
  lldb/packages/Python/lldbsuite/test/decorators.py


Index: lldb/packages/Python/lldbsuite/test/decorators.py
===================================================================
--- lldb/packages/Python/lldbsuite/test/decorators.py
+++ lldb/packages/Python/lldbsuite/test/decorators.py
@@ -86,6 +86,20 @@
     else:
         return expected == actual
 
+
+def _compiler_supports(compiler, flag):
+    """Test whether the compiler supports the given flag."""
+    if platform.system() == 'Darwin':
+        compiler = "xcrun " + compiler
+    f = tempfile.NamedTemporaryFile()
+    try:
+        cmd = "echo 'int main() {}' | %s %s -x c -o %s -" % (compiler, flag, f.name)
+        subprocess.check_call(cmd, shell=True)
+    except subprocess.CalledProcessError:
+        return False
+    return True
+
+
 def expectedFailure(func):
     return unittest2.expectedFailure(func)
 
@@ -729,12 +743,7 @@
         # rdar://28659145 - TSAN tests don't look like they're supported on i386
         if self.getArchitecture() == 'i386' and platform.system() == 'Darwin':
             return "TSAN tests not compatible with i386 targets"
-        f = tempfile.NamedTemporaryFile()
-        cmd = "echo 'int main() {}' | %s -x c -o %s -" % (compiler_path, f.name)
-        if os.popen(cmd).close() is not None:
-            return None  # The compiler cannot compile at all, let's *not* skip the test
-        cmd = "echo 'int main() {}' | %s -fsanitize=thread -x c -o %s -" % (compiler_path, f.name)
-        if os.popen(cmd).close() is not None:
+        if not _compiler_supports(compiler_path, '-fsanitize=thread'):
             return "Compiler cannot compile with -fsanitize=thread"
         return None
     return skipTestIfFn(is_compiler_clang_with_thread_sanitizer)(func)
@@ -755,8 +764,7 @@
         outputf = tempfile.NamedTemporaryFile()
 
         # Try to compile with ubsan turned on.
-        cmd = '%s -fsanitize=undefined %s -o %s' % (self.getCompiler(), inputf.name, outputf.name)
-        if os.popen(cmd).close() is not None:
+        if not _compiler_supports(self.getCompiler(), '-fsanitize=undefined'):
             return "Compiler cannot compile with -fsanitize=undefined"
 
         # Check that we actually see ubsan instrumentation in the binary.
@@ -804,16 +812,9 @@
         if is_running_under_asan():
             return "Address sanitizer tests are disabled when runing under ASAN"
 
-        compiler_path = self.getCompiler()
-        compiler = os.path.basename(compiler_path)
-        f = tempfile.NamedTemporaryFile()
         if lldbplatformutil.getPlatform() == 'windows':
             return "ASAN tests not compatible with 'windows'"
-        cmd = "echo 'int main() {}' | %s -x c -o %s -" % (compiler_path, f.name)
-        if os.popen(cmd).close() is not None:
-            return None  # The compiler cannot compile at all, let's *not* skip the test
-        cmd = "echo 'int main() {}' | %s -fsanitize=address -x c -o %s -" % (compiler_path, f.name)
-        if os.popen(cmd).close() is not None:
+        if not _compiler_supports(self.getCompiler(), '-fsanitize=address'):
             return "Compiler cannot compile with -fsanitize=address"
         return None
     return skipTestIfFn(is_compiler_with_address_sanitizer)(func)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D95995.321294.patch
Type: text/x-patch
Size: 3218 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20210204/a062f320/attachment.bin>


More information about the lldb-commits mailing list