Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1# Copyright 2017 The CRC32C Authors. All rights reserved.2# Use of this source code is governed by a BSD-style license that can be3# found in the LICENSE file.4"""YouCompleteMe configuration that interprets a .clang_complete file.6This module implementes the YouCompleteMe configuration API documented at:7https://github.com/ycm-core/ycmd#ycm_extra_confpy-specification9The implementation loads and processes a .clang_complete file, documented at:10https://github.com/xavierd/clang_complete/blob/master/README.md11"""13import os15# Flags added to the list in .clang_complete.16BASE_FLAGS = [17'-Werror', # Unlike clang_complete, YCM can also be used as a linter.18'-DUSE_CLANG_COMPLETER', # YCM needs this.19'-xc++', # YCM needs this to avoid compiling headers as C code.20]22# Clang flags that take in paths.23# See https://clang.llvm.org/docs/ClangCommandLineReference.html24PATH_FLAGS = [25'-isystem',26'-I',27'-iquote',28'--sysroot='29]32def DirectoryOfThisScript():33"""Returns the absolute path to the directory containing this script."""34return os.path.dirname(os.path.abspath(__file__))37def MakeRelativePathsInFlagsAbsolute(flags, build_root):38"""Expands relative paths in a list of Clang command-line flags.40Args:41flags: The list of flags passed to Clang.42build_root: The current directory when running the Clang compiler. Should be43an absolute path.45Returns:46A list of flags with relative paths replaced by absolute paths.47"""48new_flags = []49make_next_absolute = False50for flag in flags:51new_flag = flag53if make_next_absolute:54make_next_absolute = False55if not flag.startswith('/'):56new_flag = os.path.join(build_root, flag)58for path_flag in PATH_FLAGS:59if flag == path_flag:60make_next_absolute = True61break63if flag.startswith(path_flag):64path = flag[len(path_flag):]65new_flag = path_flag + os.path.join(build_root, path)66break68if new_flag:69new_flags.append(new_flag)70return new_flags73def FindNearest(target, path, build_root):74"""Looks for a file with a specific name closest to a project path.76This is similar to the logic used by a version-control system (like git) to77find its configuration directory (.git) based on the current directory when a78command is invoked.80Args:81target: The file name to search for.82path: The directory where the search starts. The search will explore the83given directory's ascendants using the parent relationship. Should be an84absolute path.85build_root: A directory that acts as a fence for the search. If the search86reaches this directory, it will not advance to its parent. Should be an87absolute path.89Returns:90The path to a file with the desired name. None if the search failed.91"""92candidate = os.path.join(path, target)93if os.path.isfile(candidate):94return candidate96if path == build_root:97return None99parent = os.path.dirname(path)100if parent == path:101return None103return FindNearest(target, parent, build_root)106def FlagsForClangComplete(file_path, build_root):107"""Reads the .clang_complete flags for a source file.109Args:110file_path: The path to the source file. Should be inside the project. Used111to locate the relevant .clang_complete file.112build_root: The current directory when running the Clang compiler for this113file. Should be an absolute path.115Returns:116A list of strings, where each element is a Clang command-line flag.117"""118clang_complete_path = FindNearest('.clang_complete', file_path, build_root)119if clang_complete_path is None:120return None121clang_complete_flags = open(clang_complete_path, 'r').read().splitlines()122return clang_complete_flags125def FlagsForFile(filename, **kwargs):126"""Implements the YouCompleteMe API."""128# kwargs can be used to pass 'client_data' to the YCM configuration. This129# configuration script does not need any extra information, so130# pylint: disable=unused-argument132build_root = DirectoryOfThisScript()133file_path = os.path.realpath(filename)135flags = BASE_FLAGS136clang_flags = FlagsForClangComplete(file_path, build_root)137if clang_flags:138flags += clang_flags140final_flags = MakeRelativePathsInFlagsAbsolute(flags, build_root)142return {'flags': final_flags}
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.