OLD | NEW |
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 # Autocompletion config for YouCompleteMe in Chromium. | 5 # Autocompletion config for YouCompleteMe in Chromium. |
6 # | 6 # |
7 # USAGE: | 7 # USAGE: |
8 # | 8 # |
9 # 1. Install YCM [https://github.com/Valloric/YouCompleteMe] | 9 # 1. Install YCM [https://github.com/Valloric/YouCompleteMe] |
10 # (Googlers should check out [go/ycm]) | 10 # (Googlers should check out [go/ycm]) |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 import sys | 56 import sys |
57 | 57 |
58 # Flags from YCM's default config. | 58 # Flags from YCM's default config. |
59 _default_flags = [ | 59 _default_flags = [ |
60 '-DUSE_CLANG_COMPLETER', | 60 '-DUSE_CLANG_COMPLETER', |
61 '-std=c++11', | 61 '-std=c++11', |
62 '-x', | 62 '-x', |
63 'c++', | 63 'c++', |
64 ] | 64 ] |
65 | 65 |
| 66 _header_alternates = ('.cc', '.cpp', '.c', '.mm', '.m') |
| 67 |
66 _extension_flags = { | 68 _extension_flags = { |
67 '.m': ['-x', 'objective-c'], | 69 '.m': ['-x', 'objective-c'], |
68 '.mm': ['-x', 'objective-c++'], | 70 '.mm': ['-x', 'objective-c++'], |
69 } | 71 } |
70 | 72 |
71 def PathExists(*args): | 73 def PathExists(*args): |
72 return os.path.exists(os.path.join(*args)) | 74 return os.path.exists(os.path.join(*args)) |
73 | 75 |
74 | 76 |
75 def FindChromeSrcFromFilename(filename): | 77 def FindChromeSrcFromFilename(filename): |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 """ | 112 """ |
111 blink_root = os.path.join(chrome_root, 'third_party', 'WebKit') | 113 blink_root = os.path.join(chrome_root, 'third_party', 'WebKit') |
112 if filename.startswith(blink_root): | 114 if filename.startswith(blink_root): |
113 return os.path.join(blink_root, 'Source', 'core', 'Init.cpp') | 115 return os.path.join(blink_root, 'Source', 'core', 'Init.cpp') |
114 else: | 116 else: |
115 if 'test.' in filename: | 117 if 'test.' in filename: |
116 return os.path.join(chrome_root, 'base', 'logging_unittest.cc') | 118 return os.path.join(chrome_root, 'base', 'logging_unittest.cc') |
117 return os.path.join(chrome_root, 'base', 'logging.cc') | 119 return os.path.join(chrome_root, 'base', 'logging.cc') |
118 | 120 |
119 | 121 |
120 def GetBuildableSourceFile(chrome_root, filename): | |
121 """Returns a buildable source file corresponding to |filename|. | |
122 | |
123 A buildable source file is one which is likely to be passed into clang as a | |
124 source file during the build. For .h files, returns the closest matching .cc, | |
125 .cpp or .c file. If no such file is found, returns the same as | |
126 GetDefaultSourceFile(). | |
127 | |
128 Args: | |
129 chrome_root: (String) Absolute path to the root of Chromium checkout. | |
130 filename: (String) Absolute path to the target source file. | |
131 | |
132 Returns: | |
133 (String) Absolute path to source file. | |
134 """ | |
135 if filename.endswith('.h'): | |
136 # Header files can't be built. Instead, try to match a header file to its | |
137 # corresponding source file. | |
138 alternates = ['.cc', '.cpp', '.c'] | |
139 for alt_extension in alternates: | |
140 alt_name = filename[:-2] + alt_extension | |
141 if os.path.exists(alt_name): | |
142 return alt_name | |
143 | |
144 return GetDefaultSourceFile(chrome_root, filename) | |
145 | |
146 return filename | |
147 | |
148 | |
149 def GetNinjaBuildOutputsForSourceFile(out_dir, filename): | 122 def GetNinjaBuildOutputsForSourceFile(out_dir, filename): |
150 """Returns a list of build outputs for filename. | 123 """Returns a list of build outputs for filename. |
151 | 124 |
152 The list is generated by invoking 'ninja -t query' tool to retrieve a list of | 125 The list is generated by invoking 'ninja -t query' tool to retrieve a list of |
153 inputs and outputs of |filename|. This list is then filtered to only include | 126 inputs and outputs of |filename|. This list is then filtered to only include |
154 .o and .obj outputs. | 127 .o and .obj outputs. |
155 | 128 |
156 Args: | 129 Args: |
157 out_dir: (String) Absolute path to ninja build output directory. | 130 out_dir: (String) Absolute path to ninja build output directory. |
158 filename: (String) Absolute path to source file. | 131 filename: (String) Absolute path to source file. |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
319 # Version of Clang used to compile Chromium can be newer then version of | 292 # Version of Clang used to compile Chromium can be newer then version of |
320 # libclang that YCM uses for completion. So it's possible that YCM's libclang | 293 # libclang that YCM uses for completion. So it's possible that YCM's libclang |
321 # doesn't know about some used warning options, which causes compilation | 294 # doesn't know about some used warning options, which causes compilation |
322 # warnings (and errors, because of '-Werror'); | 295 # warnings (and errors, because of '-Werror'); |
323 additional_flags.append('-Wno-unknown-warning-option') | 296 additional_flags.append('-Wno-unknown-warning-option') |
324 | 297 |
325 sys.path.append(os.path.join(chrome_root, 'tools', 'vim')) | 298 sys.path.append(os.path.join(chrome_root, 'tools', 'vim')) |
326 from ninja_output import GetNinjaOutputDirectory | 299 from ninja_output import GetNinjaOutputDirectory |
327 out_dir = GetNinjaOutputDirectory(chrome_root) | 300 out_dir = GetNinjaOutputDirectory(chrome_root) |
328 | 301 |
329 clang_line = GetClangCommandLineFromNinjaForSource( | 302 basename, extension = os.path.splitext(filename) |
330 out_dir, GetBuildableSourceFile(chrome_root, filename)) | 303 if extension == '.h': |
| 304 candidates = [basename + ext for ext in _header_alternates] |
| 305 else: |
| 306 candidates = [filename] |
| 307 |
| 308 clang_line = None |
| 309 buildable_extension = extension |
| 310 for candidate in candidates: |
| 311 clang_line = GetClangCommandLineFromNinjaForSource(out_dir, candidate) |
| 312 if clang_line: |
| 313 buildable_extension = os.path.splitext(candidate)[1] |
| 314 break |
| 315 |
| 316 additional_flags += _extension_flags.get(buildable_extension, []) |
| 317 |
331 if not clang_line: | 318 if not clang_line: |
332 # If ninja didn't know about filename or it's companion files, then try a | 319 # If ninja didn't know about filename or it's companion files, then try a |
333 # default build target. It is possible that the file is new, or build.ninja | 320 # default build target. It is possible that the file is new, or build.ninja |
334 # is stale. | 321 # is stale. |
335 clang_line = GetClangCommandLineFromNinjaForSource( | 322 clang_line = GetClangCommandLineFromNinjaForSource( |
336 out_dir, GetDefaultSourceFile(chrome_root, filename)) | 323 out_dir, GetDefaultSourceFile(chrome_root, filename)) |
337 | 324 |
338 if not clang_line: | 325 if not clang_line: |
339 return additional_flags | 326 return additional_flags |
340 | 327 |
341 return GetClangOptionsFromCommandLine(clang_line, out_dir, additional_flags) | 328 return GetClangOptionsFromCommandLine(clang_line, out_dir, additional_flags) |
342 | 329 |
343 | 330 |
344 def FlagsForFile(filename): | 331 def FlagsForFile(filename): |
345 """This is the main entry point for YCM. Its interface is fixed. | 332 """This is the main entry point for YCM. Its interface is fixed. |
346 | 333 |
347 Args: | 334 Args: |
348 filename: (String) Path to source file being edited. | 335 filename: (String) Path to source file being edited. |
349 | 336 |
350 Returns: | 337 Returns: |
351 (Dictionary) | 338 (Dictionary) |
352 'flags': (List of Strings) Command line flags. | 339 'flags': (List of Strings) Command line flags. |
353 'do_cache': (Boolean) True if the result should be cached. | 340 'do_cache': (Boolean) True if the result should be cached. |
354 """ | 341 """ |
355 ext = os.path.splitext(filename)[1] | |
356 abs_filename = os.path.abspath(filename) | 342 abs_filename = os.path.abspath(filename) |
357 chrome_root = FindChromeSrcFromFilename(abs_filename) | 343 chrome_root = FindChromeSrcFromFilename(abs_filename) |
358 clang_flags = GetClangOptionsFromNinjaForFilename(chrome_root, abs_filename) | 344 clang_flags = GetClangOptionsFromNinjaForFilename(chrome_root, abs_filename) |
359 | 345 |
360 # If clang_flags could not be determined, then assume that was due to a | 346 # If clang_flags could not be determined, then assume that was due to a |
361 # transient failure. Preventing YCM from caching the flags allows us to try to | 347 # transient failure. Preventing YCM from caching the flags allows us to try to |
362 # determine the flags again. | 348 # determine the flags again. |
363 should_cache_flags_for_file = bool(clang_flags) | 349 should_cache_flags_for_file = bool(clang_flags) |
364 | 350 |
365 final_flags = _default_flags + _extension_flags.get(ext, []) + clang_flags | 351 final_flags = _default_flags + clang_flags |
366 | 352 |
367 return { | 353 return { |
368 'flags': final_flags, | 354 'flags': final_flags, |
369 'do_cache': should_cache_flags_for_file | 355 'do_cache': should_cache_flags_for_file |
370 } | 356 } |
OLD | NEW |