# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

# Autocompletion config for YouCompleteMe in Chromium.
#
# USAGE:
#
#   1. Install YCM [https://github.com/Valloric/YouCompleteMe]
#          (Googlers should check out [go/ycm])
#
#   2. Point to this config file in your .vimrc:
#          let g:ycm_global_ycm_extra_conf =
#              '<chrome_depot>/src/tools/vim/chromium.ycm_extra_conf.py'
#
#   3. Profit
#
#
# Usage notes:
#
#   * You must use ninja & clang to build Chromium.
#
#   * You must have run gyp_chromium and built Chromium recently.
#
#
# Hacking notes:
#
#   * The purpose of this script is to construct an accurate enough command line
#     for YCM to pass to clang so it can build and extract the symbols.
#
#   * Right now, we only pull the -I and -D flags. That seems to be sufficient
#     for everything I've used it for.
#
#   * That whole ninja & clang thing? We could support other configs if someone
#     were willing to write the correct commands and a parser.
#
#   * This has only been tested on gPrecise.


import os
import os.path
import re
import subprocess
import sys


def SystemIncludeDirectoryFlags():
  """Determines compile flags to include the system include directories.

  Use as a workaround for https://github.com/Valloric/YouCompleteMe/issues/303

  Returns:
    (List of Strings) Compile flags to append.
  """
  try:
    with open(os.devnull, 'rb') as DEVNULL:
      output = subprocess.check_output(['clang', '-v', '-E', '-x', 'c++', '-'],
                                       stdin=DEVNULL, stderr=subprocess.STDOUT)
  except:
    return []
  includes_regex = r'#include <\.\.\.> search starts here:\s*' \