Description: Use Debian's llvm and clang
 While this patch is mostly about dropping the usage of the embedded clang
 sources (which we strip from the upstream tarball as well) it also adapts
 (so in effect mostly reverts) changes to the tests to have them support
 multiple clang versions
Forwarded: not-needed

--- a/cpp/ycm/CMakeLists.txt
+++ b/cpp/ycm/CMakeLists.txt
@@ -172,7 +172,7 @@
   endif()
 
   # Need TEMP because find_library does not work with an option variable
-  find_library( TEMP NAMES clang libclang
+  find_library( TEMP NAMES clang libclang.so.1
                 PATHS ${LIBCLANG_SEARCH_PATH}
                 NO_DEFAULT_PATH )
   set( EXTERNAL_LIBCLANG_PATH ${TEMP} )
@@ -347,38 +347,6 @@
                        PUBLIC ${EXTRA_LIBS}
                      )
 
-if( LIBCLANG_TARGET )
-  set( LIBCLANG_DIR "${CMAKE_SOURCE_DIR}/../third_party/clang/lib" )
-  # add rpath to libclang and remove rpath to temporary directory
-  if ( NOT MSVC )
-    set_target_properties( ${PROJECT_NAME} PROPERTIES
-      BUILD_WITH_INSTALL_RPATH ON
-      SKIP_BUILD_RPATH FALSE
-      INSTALL_RPATH ${LIBCLANG_DIR} )
-  endif ()
-  # Remove all previous libclang libraries.
-  file( GLOB LIBCLANG_FILEPATHS "${LIBCLANG_DIR}/libclang*" )
-  foreach( FILEPATH ${LIBCLANG_FILEPATHS} )
-    file( REMOVE ${FILEPATH} )
-  endforeach()
-  # When building with MSVC, we need to copy libclang.dll instead of libclang.lib
-  if( MSVC )
-    add_custom_command(
-      TARGET ${PROJECT_NAME}
-      POST_BUILD
-      COMMAND ${CMAKE_COMMAND} -E copy "${PATH_TO_LLVM_ROOT}/bin/libclang.dll" "${LIBCLANG_DIR}"
-    )
-  else()
-    foreach( LIBCLANG_FILE ${PATH_TO_LIBCLANG_WITH_SYMLINKS} )
-      add_custom_command(
-        TARGET ${PROJECT_NAME}
-        POST_BUILD
-        COMMAND cp -a ${LIBCLANG_FILE} "${LIBCLANG_DIR}"
-      )
-    endforeach()
-  endif()
-endif()
-
 
 #############################################################################
 
--- a/run_tests.py
+++ b/run_tests.py
@@ -14,7 +14,6 @@
 DIR_OF_THIS_SCRIPT = p.dirname( p.abspath( __file__ ) )
 DIR_OF_THIRD_PARTY = p.join( DIR_OF_THIS_SCRIPT, 'third_party' )
 DIR_OF_WATCHDOG_DEPS = p.join( DIR_OF_THIRD_PARTY, 'watchdog_deps' )
-LIBCLANG_DIR = p.join( DIR_OF_THIRD_PARTY, 'clang', 'lib' )
 
 python_path = [
   p.join( DIR_OF_THIRD_PARTY, 'regex-build' ),
@@ -229,7 +228,6 @@
     # unittest_args += [ '-m', 'not valgrind_skip' ]
 
   new_env = os.environ.copy()
-  new_env[ 'LD_LIBRARY_PATH' ] = LIBCLANG_DIR
   new_env[ 'YCM_VALGRIND_RUN' ] = '1'
   cmd = [ 'valgrind',
           '--gen-suppressions=all',
@@ -273,13 +271,6 @@
     # Useful for _writing_ tests
     env[ 'YCM_TEST_NO_RETRY' ] = '1'
 
-  if OnWindows():
-    # We prepend the Clang third-party directory to the PATH instead of
-    # overwriting it so that the executable is able to find the Python library.
-    env[ 'PATH' ] = LIBCLANG_DIR + ';' + env[ 'PATH' ]
-  else:
-    env[ 'LD_LIBRARY_PATH' ] = LIBCLANG_DIR
-
   if parsed_args.coverage:
     executable = [ sys.executable, '-m', 'coverage', 'run' ]
   else:
--- a/ycmd/utils.py
+++ b/ycmd/utils.py
@@ -28,10 +28,6 @@
 
 LOGGER = logging.getLogger( 'ycmd' )
 ROOT_DIR = os.path.normpath( os.path.join( os.path.dirname( __file__ ), '..' ) )
-DIR_OF_THIRD_PARTY = os.path.join( ROOT_DIR, 'third_party' )
-LIBCLANG_DIR = os.path.join( DIR_OF_THIRD_PARTY, 'clang', 'lib' )
-if hasattr( os, 'add_dll_directory' ):
-  os.add_dll_directory( LIBCLANG_DIR )
 
 
 from collections.abc import Mapping
@@ -477,16 +473,6 @@
   return int( ReadFile( os.path.join( ROOT_DIR, 'CORE_VERSION' ) ) )
 
 
-def LoadYcmCoreDependencies():
-  for name in ListDirectory( LIBCLANG_DIR ):
-    if name.startswith( 'libclang' ):
-      libclang_path = os.path.join( LIBCLANG_DIR, name )
-      if os.path.isfile( libclang_path ):
-        import ctypes
-        ctypes.cdll.LoadLibrary( libclang_path )
-        return
-
-
 def ImportCore():
   """Imports and returns the ycm_core module. This function exists for easily
   mocking this import in tests."""
@@ -498,11 +484,7 @@
   """Checks if ycm_core library is compatible and returns with an exit
   status."""
   try:
-    try:
-      ycm_core = ImportCore()
-    except ImportError:
-      LoadYcmCoreDependencies()
-      ycm_core = ImportCore()
+    ycm_core = ImportCore()
   except ImportError as error:
     message = str( error )
     if CORE_MISSING_ERROR_REGEX.match( message ):
@@ -525,11 +507,7 @@
 
 
 def GetClangResourceDir():
-  resource_dir = os.path.join( LIBCLANG_DIR, 'clang' )
-  for version in ListDirectory( resource_dir ):
-    return os.path.join( resource_dir, version )
-
-  raise RuntimeError( 'Cannot find Clang resource directory.' )
+  return '/usr/lib/ycmd/clang_includes'
 
 
 CLANG_RESOURCE_DIR = GetClangResourceDir()
--- a/ycmd/completers/cpp/clangd_completer.py
+++ b/ycmd/completers/cpp/clangd_completer.py
@@ -43,16 +43,6 @@
   '(\\s*#\\s*(?:include|import)\\s*)(?:"[^"]*|<[^>]*)' )
 NOT_CACHED = 'NOT_CACHED'
 CLANGD_COMMAND = NOT_CACHED
-PRE_BUILT_CLANGD_DIR = os.path.abspath( os.path.join(
-  os.path.dirname( __file__ ),
-  '..',
-  '..',
-  '..',
-  'third_party',
-  'clangd',
-  'output',
-  'bin' ) )
-PRE_BUILT_CLANDG_PATH = os.path.join( PRE_BUILT_CLANGD_DIR, 'clangd' )
 
 
 def ParseClangdVersion( version_str ):
@@ -79,51 +69,41 @@
   return True
 
 
-def GetThirdPartyClangd():
-  pre_built_clangd = GetExecutable( PRE_BUILT_CLANDG_PATH )
-  if not pre_built_clangd:
-    LOGGER.info( 'No Clangd executable found in %s', PRE_BUILT_CLANGD_DIR )
-    return None
-  if not CheckClangdVersion( pre_built_clangd ):
-    LOGGER.error( 'Clangd executable at %s is out-of-date', pre_built_clangd )
-    return None
-  LOGGER.info( 'Clangd executable found at %s and up to date',
-               PRE_BUILT_CLANGD_DIR )
-  return pre_built_clangd
-
-
-def GetClangdExecutableAndResourceDir( user_options ):
+def GetClangdExecutable( user_options ):
   """Return the Clangd binary from the path specified in the
   'clangd_binary_path' option. Let the binary find its resource directory in
-  that case. If no binary is found or if it's out-of-date, return nothing. If
-  'clangd_binary_path' is empty, return the third-party Clangd and its resource
-  directory if the user downloaded it and if it's up to date. Otherwise, return
-  nothing."""
-  clangd = user_options[ 'clangd_binary_path' ]
-  resource_dir = None
-
-  if clangd:
-    clangd = FindExecutable( ExpandVariablesInPath( clangd ) )
+  that case. If no binary is found or if it's out-of-date, return nothing."""
+  clangd = None
+  if user_options[ 'clangd_binary_path' ]:
+    clangd = FindExecutable( ExpandVariablesInPath( user_options[ 'clangd_binary_path' ] ) )
 
     if not clangd:
       LOGGER.error( 'No Clangd executable found at %s',
                     user_options[ 'clangd_binary_path' ] )
-      return None, None
-
-    if not CheckClangdVersion( clangd ):
-      LOGGER.error( 'Clangd at %s is out-of-date', clangd )
-      return None, None
-
-  # Try looking for the pre-built binary.
+      return None
   else:
-    third_party_clangd = GetThirdPartyClangd()
-    if not third_party_clangd:
-      return None, None
-    clangd = third_party_clangd
-    resource_dir = CLANG_RESOURCE_DIR
+    for exe in [ '/usr/bin/clangd', ]:
+      if os.path.exists(exe):
+        clangd = exe
+        break
+    if not clangd:
+      for ver in [ 19, 18, 17, 16, 15, 14, 13, 20, 21, 22 ]:
+        if ver < MIN_SUPPORTED_VERSION[0]:
+            continue
+        exe = '/usr/bin/clangd-%d' % ver
+        if os.path.exists(exe):
+          clangd = exe
+          break
+    if not clangd:
+      LOGGER.error( 'No Clangd executable found. Try `apt install clangd` or set path explicitly' )
+      return None
+
+  if not CheckClangdVersion( clangd ):
+    LOGGER.error( 'Clangd at %s is out-of-date', clangd )
+    return None
 
   LOGGER.info( 'Using Clangd from %s', clangd )
-  return clangd, resource_dir
+  return clangd
 
 
 def GetClangdCommand( user_options ):
@@ -135,28 +115,23 @@
     return CLANGD_COMMAND
   CLANGD_COMMAND = None
 
-  installed_clangd, resource_dir = GetClangdExecutableAndResourceDir(
-      user_options )
+  installed_clangd = GetClangdExecutable( user_options )
   if not installed_clangd:
     return None
 
   CLANGD_COMMAND = [ installed_clangd ]
   clangd_args = user_options[ 'clangd_args' ]
-  put_resource_dir = False
   put_limit_results = False
   put_header_insertion_decorators = False
   put_log = False
   for arg in clangd_args:
     CLANGD_COMMAND.append( arg )
-    put_resource_dir = put_resource_dir or arg.startswith( '-resource-dir' )
     put_limit_results = put_limit_results or arg.startswith( '-limit-results' )
     put_header_insertion_decorators = ( put_header_insertion_decorators or
                         arg.startswith( '-header-insertion-decorators' ) )
     put_log = put_log or arg.startswith( '-log' )
   if not put_header_insertion_decorators:
     CLANGD_COMMAND.append( '-header-insertion-decorators=0' )
-  if resource_dir and not put_resource_dir:
-    CLANGD_COMMAND.append( '-resource-dir=' + resource_dir )
   if user_options[ 'clangd_uses_ycmd_caching' ] and not put_limit_results:
     CLANGD_COMMAND.append( '-limit-results=500' )
   if LOGGER.isEnabledFor( logging.DEBUG ) and not put_log:
--- a/ycmd/tests/utils_test.py
+++ b/ycmd/tests/utils_test.py
@@ -33,7 +33,7 @@
 from types import ModuleType
 from unittest import TestCase
 from ycmd import utils
-from ycmd.tests.test_utils import ( WindowsOnly, UnixOnly,
+from ycmd.tests.test_utils import ( WindowsOnly, UnixOnly, NotDebian,
                                     CurrentWorkingDirectory,
                                     TemporaryExecutable )
 from ycmd.tests import PathToTestFile
@@ -517,6 +517,7 @@
       'script. See the documentation for more details.' )
 
 
+  @NotDebian
   @patch( 'ycmd.utils.ListDirectory', return_value = [] )
   def test_GetClangResourceDir_NotFound( self, *args ):
     assert_that(
--- a/ycmd/tests/clangd/utilities_test.py
+++ b/ycmd/tests/clangd/utilities_test.py
@@ -25,7 +25,7 @@
 from ycmd.completers.language_server.language_server_completer import (
     LanguageServerConnectionTimeout )
 from ycmd.tests.clangd import IsolatedYcmd, PathToTestFile
-from ycmd.tests.test_utils import BuildRequest
+from ycmd.tests.test_utils import ( BuildRequest, NotDebian )
 from ycmd.user_options_store import DefaultOptions
 
 
@@ -39,6 +39,7 @@
 
 
 class UtilitiesTest( TestCase ):
+  @NotDebian
   def test_ClangdCompleter_GetClangdCommand_NoCustomBinary( self ):
     user_options = DefaultOptions()
 
@@ -123,8 +124,7 @@
     user_options = DefaultOptions()
 
     # Clangd not in third_party (or an old version).
-    with patch( 'ycmd.completers.cpp.clangd_completer.GetThirdPartyClangd',
-                return_value = None ):
+    if False:
       # Default.
       clangd_completer.CLANGD_COMMAND = clangd_completer.NOT_CACHED
       assert_that( clangd_completer.ShouldEnableClangdCompleter( user_options ),
@@ -152,8 +152,7 @@
     user_options = DefaultOptions()
 
     # Clangd in third_party with a supported version.
-    with patch( 'ycmd.completers.cpp.clangd_completer.GetThirdPartyClangd',
-                return_value = 'third_party_clangd' ):
+    if True:
       # Default.
       clangd_completer.CLANGD_COMMAND = clangd_completer.NOT_CACHED
       assert_that( clangd_completer.ShouldEnableClangdCompleter( user_options ),
@@ -213,6 +212,7 @@
         shutdown_server.assert_called()
 
 
+  @NotDebian
   def test_ClangdCompleter_GetThirdParty( self ):
     with patch( 'ycmd.completers.cpp.clangd_completer.GetExecutable',
                 return_value = None ):
--- a/ycmd/tests/clangd/subcommands_test.py
+++ b/ycmd/tests/clangd/subcommands_test.py
@@ -23,6 +23,7 @@
                        equal_to,
                        has_entries,
                        has_entry,
+                       is_in,
                        matches_regexp )
 from unittest.mock import patch
 from unittest import TestCase
@@ -1107,10 +1108,12 @@
           requests.codes.ok ],
         # from header
         [ { 'line_num': 6, 'column_num': 10 },
-          has_entry( 'detailed_info', equal_to(
+          has_entry( 'detailed_info', is_in( [
             'function docstring_from_header_file\nprovided by "docstring.h"'
             '\n\n→ void\ndocstring\n\n'
-            'void docstring_from_header_file()' ) ),
+            'void docstring_from_header_file()',
+            'function docstring_from_header_file\n\n→ void\ndocstring\n\n'
+            'void docstring_from_header_file()'] ) ),
           requests.codes.ok ],
         # no docstring
         [ { 'line_num': 7, 'column_num': 7 },
--- a/ycmd/tests/clangd/diagnostics_test.py
+++ b/ycmd/tests/clangd/diagnostics_test.py
@@ -567,30 +567,45 @@
       # Assert diagnostics
       for message in PollForMessages( app, messages_request ):
         if 'diagnostics' in message:
-          assert_that( message,
-            has_entries( { 'diagnostics': contains_exactly(
-              has_entries( {
-                'kind': equal_to( 'ERROR' ),
-                'text': "Use of undeclared identifier 'S' [undeclared_var_use]",
-                'ranges': contains_exactly( RangeMatcher(
-                  contains_string( source_file ), ( 2, 20 ), ( 2, 21 ) ) ),
-                'location': LocationMatcher(
-                  contains_string( source_file ), 2, 20 ),
-                'location_extent': RangeMatcher(
-                  contains_string( source_file ), ( 2, 20 ), ( 2, 21 ) )
-              } ),
-              has_entries( {
-                'kind': equal_to( 'WARNING' ),
-                'text': "Included header header.h is not used directly "
-                        "(fix available) [unused-includes]",
-                'ranges': contains_exactly( RangeMatcher(
-                  contains_string( source_file ), ( 1, 1 ), ( 1, 20 ) ) ),
-                'location': LocationMatcher(
-                  contains_string( source_file ), 1, 1 ),
-                'location_extent': RangeMatcher(
-                  contains_string( source_file ), ( 1, 1 ), ( 1, 20 ) )
-              } ),
-            ) } ) )
+          if len(message['diagnostics']) == 2:
+            assert_that( message,
+              has_entries( { 'diagnostics': contains_exactly(
+                has_entries( {
+                  'kind': equal_to( 'ERROR' ),
+                  'text': "Use of undeclared identifier 'S' [undeclared_var_use]",
+                  'ranges': contains_exactly( RangeMatcher(
+                    contains_string( source_file ), ( 2, 20 ), ( 2, 21 ) ) ),
+                  'location': LocationMatcher(
+                    contains_string( source_file ), 2, 20 ),
+                  'location_extent': RangeMatcher(
+                    contains_string( source_file ), ( 2, 20 ), ( 2, 21 ) )
+                } ),
+                has_entries( {
+                  'kind': equal_to( 'WARNING' ),
+                  'text': "Included header header.h is not used directly "
+                          "(fix available) [unused-includes]",
+                  'ranges': contains_exactly( RangeMatcher(
+                    contains_string( source_file ), ( 1, 1 ), ( 1, 20 ) ) ),
+                  'location': LocationMatcher(
+                    contains_string( source_file ), 1, 1 ),
+                  'location_extent': RangeMatcher(
+                    contains_string( source_file ), ( 1, 1 ), ( 1, 20 ) )
+                } ),
+              ) } ) )
+          else:
+            assert_that( message,
+              has_entries( { 'diagnostics': contains_exactly(
+                has_entries( {
+                  'kind': equal_to( 'ERROR' ),
+                  'text': "Use of undeclared identifier 'S' [undeclared_var_use]",
+                  'ranges': contains_exactly( RangeMatcher(
+                    contains_string( source_file ), ( 2, 20 ), ( 2, 21 ) ) ),
+                  'location': LocationMatcher(
+                    contains_string( source_file ), 2, 20 ),
+                  'location_extent': RangeMatcher(
+                    contains_string( source_file ), ( 2, 20 ), ( 2, 21 ) )
+                } ),
+              ) } ) )
           break
 
       # Restore original content
