Sync: Display non-severe errors on about:sync in gray color rather than red.
[chromium-blink-merge.git] / tools / vim / chromium.ycm_extra_conf.py
blob71863115871ef2288cc612705101b9f42af5c61e
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.
5 # Autocompletion config for YouCompleteMe in Chromium.
7 # USAGE:
9 # 1. Install YCM [https://github.com/Valloric/YouCompleteMe]
10 # (Googlers should check out [go/ycm])
12 # 2. Point to this config file in your .vimrc:
13 # let g:ycm_global_ycm_extra_conf =
14 # '<chrome_depot>/src/tools/vim/chromium.ycm_extra_conf.py'
16 # 3. Profit
19 # Usage notes:
21 # * You must use ninja & clang to build Chromium.
23 # * You must have run gyp_chromium and built Chromium recently.
26 # Hacking notes:
28 # * The purpose of this script is to construct an accurate enough command line
29 # for YCM to pass to clang so it can build and extract the symbols.
31 # * Right now, we only pull the -I and -D flags. That seems to be sufficient
32 # for everything I've used it for.
34 # * That whole ninja & clang thing? We could support other configs if someone
35 # were willing to write the correct commands and a parser.
37 # * This has only been tested on gPrecise.
40 import os
41 import subprocess
44 # Flags from YCM's default config.
45 flags = [
46 '-DUSE_CLANG_COMPLETER',
47 '-std=c++11',
48 '-x',
49 'c++',
53 def PathExists(*args):
54 return os.path.exists(os.path.join(*args))
57 def FindChromeSrcFromFilename(filename):
58 """Searches for the root of the Chromium checkout.
60 Simply checks parent directories until it finds .gclient and src/.
62 Args:
63 filename: (String) Path to source file being edited.
65 Returns:
66 (String) Path of 'src/', or None if unable to find.
67 """
68 curdir = os.path.normpath(os.path.dirname(filename))
69 while not (PathExists(curdir, 'src') and PathExists(curdir, 'src', 'DEPS')
70 and (PathExists(curdir, '.gclient')
71 or PathExists(curdir, 'src', '.git'))):
72 nextdir = os.path.normpath(os.path.join(curdir, '..'))
73 if nextdir == curdir:
74 return None
75 curdir = nextdir
76 return os.path.join(curdir, 'src')
79 # Largely copied from ninja-build.vim (guess_configuration)
80 def GetNinjaOutputDirectory(chrome_root):
81 """Returns <chrome_root>/<output_dir>/(Release|Debug).
83 The configuration chosen is the one most recently generated/built. Detects
84 a custom output_dir specified by GYP_GENERATOR_FLAGS."""
86 output_dir = 'out'
87 generator_flags = os.getenv('GYP_GENERATOR_FLAGS', '').split(' ')
88 for flag in generator_flags:
89 name_value = flag.split('=', 1)
90 if len(name_value) == 2 and name_value[0] == 'output_dir':
91 output_dir = name_value[1]
93 root = os.path.join(chrome_root, output_dir)
94 debug_path = os.path.join(root, 'Debug')
95 release_path = os.path.join(root, 'Release')
97 def is_release_15s_newer(test_path):
98 try:
99 debug_mtime = os.path.getmtime(os.path.join(debug_path, test_path))
100 except os.error:
101 debug_mtime = 0
102 try:
103 rel_mtime = os.path.getmtime(os.path.join(release_path, test_path))
104 except os.error:
105 rel_mtime = 0
106 return rel_mtime - debug_mtime >= 15
108 if is_release_15s_newer('build.ninja') or is_release_15s_newer('protoc'):
109 return release_path
110 return debug_path
113 def GetClangCommandFromNinjaForFilename(chrome_root, filename):
114 """Returns the command line to build |filename|.
116 Asks ninja how it would build the source file. If the specified file is a
117 header, tries to find its companion source file first.
119 Args:
120 chrome_root: (String) Path to src/.
121 filename: (String) Path to source file being edited.
123 Returns:
124 (List of Strings) Command line arguments for clang.
126 if not chrome_root:
127 return []
129 # Generally, everyone benefits from including Chromium's src/, because all of
130 # Chromium's includes are relative to that.
131 chrome_flags = ['-I' + os.path.join(chrome_root)]
133 # Default file to get a reasonable approximation of the flags for a Blink
134 # file.
135 blink_root = os.path.join(chrome_root, 'third_party', 'WebKit')
136 default_blink_file = os.path.join(blink_root, 'Source', 'core', 'Init.cpp')
138 # Header files can't be built. Instead, try to match a header file to its
139 # corresponding source file.
140 if filename.endswith('.h'):
141 # Add config.h to Blink headers, which won't have it by default.
142 if filename.startswith(blink_root):
143 chrome_flags.append('-include')
144 chrome_flags.append(os.path.join(blink_root, 'Source', 'config.h'))
146 alternates = ['.cc', '.cpp']
147 for alt_extension in alternates:
148 alt_name = filename[:-2] + alt_extension
149 if os.path.exists(alt_name):
150 filename = alt_name
151 break
152 else:
153 if filename.startswith(blink_root):
154 # If this is a Blink file, we can at least try to get a reasonable
155 # approximation.
156 filename = default_blink_file
157 else:
158 # If this is a standalone .h file with no source, the best we can do is
159 # try to use the default flags.
160 return chrome_flags
162 # Ninja needs the path to the source file from the output build directory.
163 # Cut off the common part and /.
164 subdir_filename = filename[len(chrome_root)+1:]
165 rel_filename = os.path.join('..', '..', subdir_filename)
167 out_dir = GetNinjaOutputDirectory(chrome_root)
169 # Ask ninja how it would build our source file.
170 p = subprocess.Popen(['ninja', '-v', '-C', out_dir, '-t',
171 'commands', rel_filename + '^'],
172 stdout=subprocess.PIPE)
173 stdout, stderr = p.communicate()
174 if p.returncode:
175 return chrome_flags
177 # Ninja might execute several commands to build something. We want the last
178 # clang command.
179 clang_line = None
180 for line in reversed(stdout.split('\n')):
181 if 'clang' in line:
182 clang_line = line
183 break
184 else:
185 return chrome_flags
187 # Parse flags that are important for YCM's purposes.
188 for flag in clang_line.split(' '):
189 if flag.startswith('-I'):
190 # Relative paths need to be resolved, because they're relative to the
191 # output dir, not the source.
192 if flag[2] == '/':
193 chrome_flags.append(flag)
194 else:
195 abs_path = os.path.normpath(os.path.join(out_dir, flag[2:]))
196 chrome_flags.append('-I' + abs_path)
197 elif flag.startswith('-std'):
198 chrome_flags.append(flag)
199 elif flag.startswith('-') and flag[1] in 'DWFfmO':
200 if flag == '-Wno-deprecated-register' or flag == '-Wno-header-guard':
201 # These flags causes libclang (3.3) to crash. Remove it until things
202 # are fixed.
203 continue
204 chrome_flags.append(flag)
206 return chrome_flags
209 def FlagsForFile(filename):
210 """This is the main entry point for YCM. Its interface is fixed.
212 Args:
213 filename: (String) Path to source file being edited.
215 Returns:
216 (Dictionary)
217 'flags': (List of Strings) Command line flags.
218 'do_cache': (Boolean) True if the result should be cached.
220 chrome_root = FindChromeSrcFromFilename(filename)
221 chrome_flags = GetClangCommandFromNinjaForFilename(chrome_root,
222 filename)
223 final_flags = flags + chrome_flags
225 return {
226 'flags': final_flags,
227 'do_cache': True