[email protected] | a08a428 | 2013-02-05 23:45:52 | [diff] [blame] | 1 | # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | # Autocompletion config for YouCompleteMe in Chromium. |
| 6 | # |
| 7 | # USAGE: |
| 8 | # |
| 9 | # 1. Install YCM [https://github.com/Valloric/YouCompleteMe] |
| 10 | # (Googlers should check out [go/ycm]) |
| 11 | # |
asanka | 385a4faa | 2015-06-03 18:40:37 | [diff] [blame] | 12 | # 2. Create a symbolic link to this file called .ycm_extra_conf.py in the |
| 13 | # directory above your Chromium checkout (i.e. next to your .gclient file). |
[email protected] | a08a428 | 2013-02-05 23:45:52 | [diff] [blame] | 14 | # |
asanka | 385a4faa | 2015-06-03 18:40:37 | [diff] [blame] | 15 | # cd src |
| 16 | # ln -rs tools/vim/chromium.ycm_extra_conf.py ../.ycm_extra_conf.py |
| 17 | # |
| 18 | # 3. (optional) Whitelist the .ycm_extra_conf.py from step #2 by adding the |
| 19 | # following to your .vimrc: |
| 20 | # |
| 21 | # let g:ycm_extra_conf_globlist=['<path to .ycm_extra_conf.py>'] |
| 22 | # |
| 23 | # You can also add other .ycm_extra_conf.py files you want to use to this |
| 24 | # list to prevent excessive prompting each time you visit a directory |
| 25 | # covered by a config file. |
| 26 | # |
| 27 | # 4. Profit |
[email protected] | a08a428 | 2013-02-05 23:45:52 | [diff] [blame] | 28 | # |
| 29 | # |
| 30 | # Usage notes: |
| 31 | # |
| 32 | # * You must use ninja & clang to build Chromium. |
| 33 | # |
Victor Costan | 3c2db7d | 2017-08-17 20:28:14 | [diff] [blame] | 34 | # * You must have built Chromium recently. |
[email protected] | a08a428 | 2013-02-05 23:45:52 | [diff] [blame] | 35 | # |
| 36 | # |
| 37 | # Hacking notes: |
| 38 | # |
| 39 | # * The purpose of this script is to construct an accurate enough command line |
| 40 | # for YCM to pass to clang so it can build and extract the symbols. |
| 41 | # |
| 42 | # * Right now, we only pull the -I and -D flags. That seems to be sufficient |
| 43 | # for everything I've used it for. |
| 44 | # |
| 45 | # * That whole ninja & clang thing? We could support other configs if someone |
| 46 | # were willing to write the correct commands and a parser. |
| 47 | # |
Sidney San Martín | 326281f | 2017-06-09 17:01:28 | [diff] [blame] | 48 | # * This has only been tested on Linux and macOS. |
[email protected] | a08a428 | 2013-02-05 23:45:52 | [diff] [blame] | 49 | |
[email protected] | a08a428 | 2013-02-05 23:45:52 | [diff] [blame] | 50 | import os |
dzhioev | 37accc2 | 2014-08-29 23:08:21 | [diff] [blame] | 51 | import os.path |
johnme | 8d6edba | 2015-01-21 14:26:28 | [diff] [blame] | 52 | import re |
asanka | 01c39d08 | 2015-05-18 20:47:11 | [diff] [blame] | 53 | import shlex |
[email protected] | a08a428 | 2013-02-05 23:45:52 | [diff] [blame] | 54 | import subprocess |
rouslan | d026bc9 | 2014-10-29 17:54:01 | [diff] [blame] | 55 | import sys |
[email protected] | a08a428 | 2013-02-05 23:45:52 | [diff] [blame] | 56 | |
| 57 | # Flags from YCM's default config. |
asanka | 01c39d08 | 2015-05-18 20:47:11 | [diff] [blame] | 58 | _default_flags = [ |
asanka | c68cb71 | 2017-03-11 17:53:53 | [diff] [blame] | 59 | '-DUSE_CLANG_COMPLETER', |
Victor Costan | 3c2db7d | 2017-08-17 20:28:14 | [diff] [blame] | 60 | '-std=c++14', |
asanka | c68cb71 | 2017-03-11 17:53:53 | [diff] [blame] | 61 | '-x', |
| 62 | 'c++', |
[email protected] | a08a428 | 2013-02-05 23:45:52 | [diff] [blame] | 63 | ] |
| 64 | |
sdy | d6a6982 | 2016-11-07 19:52:34 | [diff] [blame] | 65 | _header_alternates = ('.cc', '.cpp', '.c', '.mm', '.m') |
| 66 | |
sdy | de05ce9 | 2016-09-09 22:19:14 | [diff] [blame] | 67 | _extension_flags = { |
asanka | c68cb71 | 2017-03-11 17:53:53 | [diff] [blame] | 68 | '.m': ['-x', 'objective-c'], |
| 69 | '.mm': ['-x', 'objective-c++'], |
sdy | de05ce9 | 2016-09-09 22:19:14 | [diff] [blame] | 70 | } |
asanka | 3346ab66 | 2015-06-03 00:27:17 | [diff] [blame] | 71 | |
asanka | c68cb71 | 2017-03-11 17:53:53 | [diff] [blame] | 72 | |
[email protected] | 1cab7568 | 2013-04-24 19:50:49 | [diff] [blame] | 73 | def PathExists(*args): |
| 74 | return os.path.exists(os.path.join(*args)) |
| 75 | |
| 76 | |
[email protected] | a08a428 | 2013-02-05 23:45:52 | [diff] [blame] | 77 | def FindChromeSrcFromFilename(filename): |
| 78 | """Searches for the root of the Chromium checkout. |
| 79 | |
| 80 | Simply checks parent directories until it finds .gclient and src/. |
| 81 | |
| 82 | Args: |
| 83 | filename: (String) Path to source file being edited. |
| 84 | |
| 85 | Returns: |
| 86 | (String) Path of 'src/', or None if unable to find. |
| 87 | """ |
| 88 | curdir = os.path.normpath(os.path.dirname(filename)) |
asanka | c68cb71 | 2017-03-11 17:53:53 | [diff] [blame] | 89 | while not ( |
| 90 | os.path.basename(curdir) == 'src' and PathExists(curdir, 'DEPS') and |
| 91 | (PathExists(curdir, '..', '.gclient') or PathExists(curdir, '.git'))): |
[email protected] | a08a428 | 2013-02-05 23:45:52 | [diff] [blame] | 92 | nextdir = os.path.normpath(os.path.join(curdir, '..')) |
| 93 | if nextdir == curdir: |
| 94 | return None |
| 95 | curdir = nextdir |
johnme | 956993f | 2015-01-21 14:29:43 | [diff] [blame] | 96 | return curdir |
[email protected] | a08a428 | 2013-02-05 23:45:52 | [diff] [blame] | 97 | |
| 98 | |
asanka | 234b8d2 | 2015-06-26 22:40:23 | [diff] [blame] | 99 | def GetDefaultSourceFile(chrome_root, filename): |
| 100 | """Returns the default source file to use as an alternative to |filename|. |
[email protected] | a08a428 | 2013-02-05 23:45:52 | [diff] [blame] | 101 | |
asanka | 234b8d2 | 2015-06-26 22:40:23 | [diff] [blame] | 102 | Compile flags used to build the default source file is assumed to be a |
| 103 | close-enough approximation for building |filename|. |
[email protected] | a08a428 | 2013-02-05 23:45:52 | [diff] [blame] | 104 | |
| 105 | Args: |
asanka | 3346ab66 | 2015-06-03 00:27:17 | [diff] [blame] | 106 | chrome_root: (String) Absolute path to the root of Chromium checkout. |
asanka | 234b8d2 | 2015-06-26 22:40:23 | [diff] [blame] | 107 | filename: (String) Absolute path to the source file. |
[email protected] | a08a428 | 2013-02-05 23:45:52 | [diff] [blame] | 108 | |
| 109 | Returns: |
asanka | 234b8d2 | 2015-06-26 22:40:23 | [diff] [blame] | 110 | (String) Absolute path to substitute source file. |
[email protected] | a08a428 | 2013-02-05 23:45:52 | [diff] [blame] | 111 | """ |
[email protected] | 3025ebc4 | 2014-07-08 15:37:22 | [diff] [blame] | 112 | blink_root = os.path.join(chrome_root, 'third_party', 'WebKit') |
asanka | 3346ab66 | 2015-06-03 00:27:17 | [diff] [blame] | 113 | if filename.startswith(blink_root): |
Victor Costan | 3c2db7d | 2017-08-17 20:28:14 | [diff] [blame] | 114 | return os.path.join(blink_root, 'Source', 'core', 'CoreInitializer.cpp') |
asanka | 3346ab66 | 2015-06-03 00:27:17 | [diff] [blame] | 115 | else: |
asanka | 7dc195b1 | 2016-06-02 15:11:09 | [diff] [blame] | 116 | if 'test.' in filename: |
| 117 | return os.path.join(chrome_root, 'base', 'logging_unittest.cc') |
asanka | 3346ab66 | 2015-06-03 00:27:17 | [diff] [blame] | 118 | return os.path.join(chrome_root, 'base', 'logging.cc') |
[email protected] | 3025ebc4 | 2014-07-08 15:37:22 | [diff] [blame] | 119 | |
asanka | 3346ab66 | 2015-06-03 00:27:17 | [diff] [blame] | 120 | |
asanka | 234b8d2 | 2015-06-26 22:40:23 | [diff] [blame] | 121 | def GetNinjaBuildOutputsForSourceFile(out_dir, filename): |
| 122 | """Returns a list of build outputs for filename. |
asanka | 3346ab66 | 2015-06-03 00:27:17 | [diff] [blame] | 123 | |
asanka | 234b8d2 | 2015-06-26 22:40:23 | [diff] [blame] | 124 | The list is generated by invoking 'ninja -t query' tool to retrieve a list of |
| 125 | inputs and outputs of |filename|. This list is then filtered to only include |
| 126 | .o and .obj outputs. |
asanka | 3346ab66 | 2015-06-03 00:27:17 | [diff] [blame] | 127 | |
| 128 | Args: |
| 129 | out_dir: (String) Absolute path to ninja build output directory. |
| 130 | filename: (String) Absolute path to source file. |
| 131 | |
| 132 | Returns: |
asanka | 234b8d2 | 2015-06-26 22:40:23 | [diff] [blame] | 133 | (List of Strings) List of target names. Will return [] if |filename| doesn't |
| 134 | yield any .o or .obj outputs. |
asanka | 3346ab66 | 2015-06-03 00:27:17 | [diff] [blame] | 135 | """ |
dzhioev | 37accc2 | 2014-08-29 23:08:21 | [diff] [blame] | 136 | # Ninja needs the path to the source file relative to the output build |
| 137 | # directory. |
sdy | 50d5177 | 2016-09-09 21:36:50 | [diff] [blame] | 138 | rel_filename = os.path.relpath(filename, out_dir) |
dzhioev | 37accc2 | 2014-08-29 23:08:21 | [diff] [blame] | 139 | |
asanka | c68cb71 | 2017-03-11 17:53:53 | [diff] [blame] | 140 | p = subprocess.Popen( |
| 141 | ['ninja', '-C', out_dir, '-t', 'query', rel_filename], |
| 142 | stdout=subprocess.PIPE, |
| 143 | stderr=subprocess.STDOUT, |
| 144 | universal_newlines=True) |
asanka | 234b8d2 | 2015-06-26 22:40:23 | [diff] [blame] | 145 | stdout, _ = p.communicate() |
pastarmovj | 7e3be85f | 2016-06-07 17:53:58 | [diff] [blame] | 146 | if p.returncode != 0: |
asanka | 234b8d2 | 2015-06-26 22:40:23 | [diff] [blame] | 147 | return [] |
| 148 | |
| 149 | # The output looks like: |
| 150 | # ../../relative/path/to/source.cc: |
| 151 | # outputs: |
| 152 | # obj/reative/path/to/target.source.o |
| 153 | # obj/some/other/target2.source.o |
| 154 | # another/target.txt |
| 155 | # |
| 156 | outputs_text = stdout.partition('\n outputs:\n')[2] |
| 157 | output_lines = [line.strip() for line in outputs_text.split('\n')] |
asanka | c68cb71 | 2017-03-11 17:53:53 | [diff] [blame] | 158 | return [ |
| 159 | target for target in output_lines |
| 160 | if target and (target.endswith('.o') or target.endswith('.obj')) |
| 161 | ] |
asanka | 234b8d2 | 2015-06-26 22:40:23 | [diff] [blame] | 162 | |
| 163 | |
| 164 | def GetClangCommandLineForNinjaOutput(out_dir, build_target): |
| 165 | """Returns the Clang command line for building |build_target| |
| 166 | |
| 167 | Asks ninja for the list of commands used to build |filename| and returns the |
| 168 | final Clang invocation. |
| 169 | |
| 170 | Args: |
| 171 | out_dir: (String) Absolute path to ninja build output directory. |
| 172 | build_target: (String) A build target understood by ninja |
| 173 | |
| 174 | Returns: |
| 175 | (String or None) Clang command line or None if a Clang command line couldn't |
| 176 | be determined. |
| 177 | """ |
asanka | c68cb71 | 2017-03-11 17:53:53 | [diff] [blame] | 178 | p = subprocess.Popen( |
| 179 | ['ninja', '-v', '-C', out_dir, '-t', 'commands', build_target], |
| 180 | stdout=subprocess.PIPE, |
| 181 | universal_newlines=True) |
[email protected] | a08a428 | 2013-02-05 23:45:52 | [diff] [blame] | 182 | stdout, stderr = p.communicate() |
pastarmovj | 7e3be85f | 2016-06-07 17:53:58 | [diff] [blame] | 183 | if p.returncode != 0: |
asanka | 3346ab66 | 2015-06-03 00:27:17 | [diff] [blame] | 184 | return None |
[email protected] | a08a428 | 2013-02-05 23:45:52 | [diff] [blame] | 185 | |
asanka | 234b8d2 | 2015-06-26 22:40:23 | [diff] [blame] | 186 | # Ninja will return multiple build steps for all dependencies up to |
| 187 | # |build_target|. The build step we want is the last Clang invocation, which |
| 188 | # is expected to be the one that outputs |build_target|. |
[email protected] | a08a428 | 2013-02-05 23:45:52 | [diff] [blame] | 189 | for line in reversed(stdout.split('\n')): |
[email protected] | eea749f | 2013-02-06 19:30:38 | [diff] [blame] | 190 | if 'clang' in line: |
asanka | 3346ab66 | 2015-06-03 00:27:17 | [diff] [blame] | 191 | return line |
| 192 | return None |
| 193 | |
| 194 | |
asanka | 234b8d2 | 2015-06-26 22:40:23 | [diff] [blame] | 195 | def GetClangCommandLineFromNinjaForSource(out_dir, filename): |
| 196 | """Returns a Clang command line used to build |filename|. |
| 197 | |
| 198 | The same source file could be built multiple times using different tool |
| 199 | chains. In such cases, this command returns the first Clang invocation. We |
| 200 | currently don't prefer one toolchain over another. Hopefully the tool chain |
| 201 | corresponding to the Clang command line is compatible with the Clang build |
| 202 | used by YCM. |
| 203 | |
| 204 | Args: |
| 205 | out_dir: (String) Absolute path to Chromium checkout. |
| 206 | filename: (String) Absolute path to source file. |
| 207 | |
| 208 | Returns: |
| 209 | (String or None): Command line for Clang invocation using |filename| as a |
| 210 | source. Returns None if no such command line could be found. |
| 211 | """ |
| 212 | build_targets = GetNinjaBuildOutputsForSourceFile(out_dir, filename) |
| 213 | for build_target in build_targets: |
| 214 | command_line = GetClangCommandLineForNinjaOutput(out_dir, build_target) |
| 215 | if command_line: |
| 216 | return command_line |
| 217 | return None |
| 218 | |
| 219 | |
asanka | 3346ab66 | 2015-06-03 00:27:17 | [diff] [blame] | 220 | def GetClangOptionsFromCommandLine(clang_commandline, out_dir, |
| 221 | additional_flags): |
| 222 | """Extracts relevant command line options from |clang_commandline| |
| 223 | |
| 224 | Args: |
| 225 | clang_commandline: (String) Full Clang invocation. |
| 226 | out_dir: (String) Absolute path to ninja build directory. Relative paths in |
| 227 | the command line are relative to |out_dir|. |
| 228 | additional_flags: (List of String) Additional flags to return. |
| 229 | |
| 230 | Returns: |
asanka | 00a5f59 | 2015-07-20 19:37:01 | [diff] [blame] | 231 | (List of Strings) The list of command line flags for this source file. Can |
| 232 | be empty. |
asanka | 3346ab66 | 2015-06-03 00:27:17 | [diff] [blame] | 233 | """ |
asanka | 00a5f59 | 2015-07-20 19:37:01 | [diff] [blame] | 234 | clang_flags = [] + additional_flags |
[email protected] | a08a428 | 2013-02-05 23:45:52 | [diff] [blame] | 235 | |
Sidney San Martín | 326281f | 2017-06-09 17:01:28 | [diff] [blame] | 236 | def abspath(path): |
| 237 | return os.path.normpath(os.path.join(out_dir, path)) |
| 238 | |
[email protected] | e666f18 | 2014-01-09 12:21:37 | [diff] [blame] | 239 | # Parse flags that are important for YCM's purposes. |
asanka | 3346ab66 | 2015-06-03 00:27:17 | [diff] [blame] | 240 | clang_tokens = shlex.split(clang_commandline) |
Asanka Herath | cc05fe7 | 2017-08-31 19:01:42 | [diff] [blame] | 241 | include_pattern = re.compile(r'^(-I|-isystem)(.+)$') |
eroman | f184b3a | 2015-08-13 19:12:38 | [diff] [blame] | 242 | for flag_index, flag in enumerate(clang_tokens): |
Jeremy Roman | 807200c | 2017-08-17 18:51:58 | [diff] [blame] | 243 | include_match = include_pattern.match(flag) |
| 244 | if include_match: |
[email protected] | a08a428 | 2013-02-05 23:45:52 | [diff] [blame] | 245 | # Relative paths need to be resolved, because they're relative to the |
| 246 | # output dir, not the source. |
Jeremy Roman | 807200c | 2017-08-17 18:51:58 | [diff] [blame] | 247 | path = abspath(include_match.group(2)) |
| 248 | clang_flags.append(include_match.group(1) + path) |
| 249 | elif flag.startswith('-std') or flag == '-nostdinc++': |
asanka | 00a5f59 | 2015-07-20 19:37:01 | [diff] [blame] | 250 | clang_flags.append(flag) |
Vitaliy Kharin | 3f33f81 | 2018-10-17 13:51:29 | [diff] [blame] | 251 | elif flag.startswith('-march=arm'): |
| 252 | # Value armv7-a of this flag causes a parsing error with a message |
| 253 | # "ClangParseError: Failed to parse the translation unit." |
| 254 | continue |
[email protected] | eea749f | 2013-02-06 19:30:38 | [diff] [blame] | 255 | elif flag.startswith('-') and flag[1] in 'DWFfmO': |
[email protected] | dc569cf9 | 2013-07-12 03:21:12 | [diff] [blame] | 256 | if flag == '-Wno-deprecated-register' or flag == '-Wno-header-guard': |
| 257 | # These flags causes libclang (3.3) to crash. Remove it until things |
[email protected] | 06a4f3e | 2013-07-09 00:16:49 | [diff] [blame] | 258 | # are fixed. |
| 259 | continue |
asanka | 00a5f59 | 2015-07-20 19:37:01 | [diff] [blame] | 260 | clang_flags.append(flag) |
Asanka Herath | cc05fe7 | 2017-08-31 19:01:42 | [diff] [blame] | 261 | elif flag == '-isysroot' or flag == '-isystem' or flag == '-I': |
eroman | f184b3a | 2015-08-13 19:12:38 | [diff] [blame] | 262 | if flag_index + 1 < len(clang_tokens): |
| 263 | clang_flags.append(flag) |
Sidney San Martín | 326281f | 2017-06-09 17:01:28 | [diff] [blame] | 264 | clang_flags.append(abspath(clang_tokens[flag_index + 1])) |
sunnyps | 3a2e1e8 | 2016-02-12 18:35:57 | [diff] [blame] | 265 | elif flag.startswith('--sysroot='): |
| 266 | # On Linux we use a sysroot image. |
| 267 | sysroot_path = flag.lstrip('--sysroot=') |
| 268 | if sysroot_path.startswith('/'): |
| 269 | clang_flags.append(flag) |
| 270 | else: |
Sidney San Martín | 326281f | 2017-06-09 17:01:28 | [diff] [blame] | 271 | clang_flags.append('--sysroot=' + abspath(sysroot_path)) |
asanka | 00a5f59 | 2015-07-20 19:37:01 | [diff] [blame] | 272 | return clang_flags |
asanka | 3346ab66 | 2015-06-03 00:27:17 | [diff] [blame] | 273 | |
| 274 | |
| 275 | def GetClangOptionsFromNinjaForFilename(chrome_root, filename): |
| 276 | """Returns the Clang command line options needed for building |filename|. |
| 277 | |
| 278 | Command line options are based on the command used by ninja for building |
| 279 | |filename|. If |filename| is a .h file, uses its companion .cc or .cpp file. |
| 280 | If a suitable companion file can't be located or if ninja doesn't know about |
| 281 | |filename|, then uses default source files in Blink and Chromium for |
| 282 | determining the commandline. |
| 283 | |
| 284 | Args: |
| 285 | chrome_root: (String) Path to src/. |
| 286 | filename: (String) Absolute path to source file being edited. |
| 287 | |
| 288 | Returns: |
asanka | 00a5f59 | 2015-07-20 19:37:01 | [diff] [blame] | 289 | (List of Strings) The list of command line flags for this source file. Can |
| 290 | be empty. |
asanka | 3346ab66 | 2015-06-03 00:27:17 | [diff] [blame] | 291 | """ |
| 292 | if not chrome_root: |
asanka | 00a5f59 | 2015-07-20 19:37:01 | [diff] [blame] | 293 | return [] |
asanka | 3346ab66 | 2015-06-03 00:27:17 | [diff] [blame] | 294 | |
| 295 | # Generally, everyone benefits from including Chromium's src/, because all of |
| 296 | # Chromium's includes are relative to that. |
| 297 | additional_flags = ['-I' + os.path.join(chrome_root)] |
| 298 | |
| 299 | # Version of Clang used to compile Chromium can be newer then version of |
| 300 | # libclang that YCM uses for completion. So it's possible that YCM's libclang |
| 301 | # doesn't know about some used warning options, which causes compilation |
| 302 | # warnings (and errors, because of '-Werror'); |
| 303 | additional_flags.append('-Wno-unknown-warning-option') |
| 304 | |
| 305 | sys.path.append(os.path.join(chrome_root, 'tools', 'vim')) |
| 306 | from ninja_output import GetNinjaOutputDirectory |
sdy | 50d5177 | 2016-09-09 21:36:50 | [diff] [blame] | 307 | out_dir = GetNinjaOutputDirectory(chrome_root) |
asanka | 3346ab66 | 2015-06-03 00:27:17 | [diff] [blame] | 308 | |
sdy | d6a6982 | 2016-11-07 19:52:34 | [diff] [blame] | 309 | basename, extension = os.path.splitext(filename) |
| 310 | if extension == '.h': |
| 311 | candidates = [basename + ext for ext in _header_alternates] |
| 312 | else: |
| 313 | candidates = [filename] |
| 314 | |
| 315 | clang_line = None |
| 316 | buildable_extension = extension |
| 317 | for candidate in candidates: |
| 318 | clang_line = GetClangCommandLineFromNinjaForSource(out_dir, candidate) |
| 319 | if clang_line: |
| 320 | buildable_extension = os.path.splitext(candidate)[1] |
| 321 | break |
| 322 | |
| 323 | additional_flags += _extension_flags.get(buildable_extension, []) |
| 324 | |
asanka | 3346ab66 | 2015-06-03 00:27:17 | [diff] [blame] | 325 | if not clang_line: |
| 326 | # If ninja didn't know about filename or it's companion files, then try a |
| 327 | # default build target. It is possible that the file is new, or build.ninja |
| 328 | # is stale. |
asanka | c68cb71 | 2017-03-11 17:53:53 | [diff] [blame] | 329 | clang_line = GetClangCommandLineFromNinjaForSource(out_dir, |
| 330 | GetDefaultSourceFile( |
| 331 | chrome_root, |
| 332 | filename)) |
asanka | 234b8d2 | 2015-06-26 22:40:23 | [diff] [blame] | 333 | |
| 334 | if not clang_line: |
pastarmovj | 7e3be85f | 2016-06-07 17:53:58 | [diff] [blame] | 335 | return additional_flags |
asanka | 3346ab66 | 2015-06-03 00:27:17 | [diff] [blame] | 336 | |
| 337 | return GetClangOptionsFromCommandLine(clang_line, out_dir, additional_flags) |
[email protected] | a08a428 | 2013-02-05 23:45:52 | [diff] [blame] | 338 | |
| 339 | |
Eric Roman | 803a136c | 2020-07-13 17:53:22 | [diff] [blame] | 340 | # FlagsForFile entrypoint is deprecated in YCM and has replaced by |
| 341 | # Settings. |
[email protected] | a08a428 | 2013-02-05 23:45:52 | [diff] [blame] | 342 | def FlagsForFile(filename): |
Eric Roman | 803a136c | 2020-07-13 17:53:22 | [diff] [blame] | 343 | """This is the old entry point for YCM. Its interface is fixed. |
[email protected] | a08a428 | 2013-02-05 23:45:52 | [diff] [blame] | 344 | |
| 345 | Args: |
| 346 | filename: (String) Path to source file being edited. |
| 347 | |
| 348 | Returns: |
| 349 | (Dictionary) |
| 350 | 'flags': (List of Strings) Command line flags. |
| 351 | 'do_cache': (Boolean) True if the result should be cached. |
| 352 | """ |
Eric Roman | 803a136c | 2020-07-13 17:53:22 | [diff] [blame] | 353 | return Settings(filename=filename) |
| 354 | |
| 355 | |
| 356 | def Settings(**kwargs): |
| 357 | filename = kwargs['filename'] |
asanka | 3346ab66 | 2015-06-03 00:27:17 | [diff] [blame] | 358 | abs_filename = os.path.abspath(filename) |
| 359 | chrome_root = FindChromeSrcFromFilename(abs_filename) |
asanka | 00a5f59 | 2015-07-20 19:37:01 | [diff] [blame] | 360 | clang_flags = GetClangOptionsFromNinjaForFilename(chrome_root, abs_filename) |
asanka | 3346ab66 | 2015-06-03 00:27:17 | [diff] [blame] | 361 | |
asanka | 00a5f59 | 2015-07-20 19:37:01 | [diff] [blame] | 362 | # If clang_flags could not be determined, then assume that was due to a |
| 363 | # transient failure. Preventing YCM from caching the flags allows us to try to |
| 364 | # determine the flags again. |
| 365 | should_cache_flags_for_file = bool(clang_flags) |
asanka | 3346ab66 | 2015-06-03 00:27:17 | [diff] [blame] | 366 | |
sdy | d6a6982 | 2016-11-07 19:52:34 | [diff] [blame] | 367 | final_flags = _default_flags + clang_flags |
[email protected] | a08a428 | 2013-02-05 23:45:52 | [diff] [blame] | 368 | |
asanka | c68cb71 | 2017-03-11 17:53:53 | [diff] [blame] | 369 | return {'flags': final_flags, 'do_cache': should_cache_flags_for_file} |