kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/cxx/extractor/CommandLineUtils.h (about)

     1  /*
     2   * Copyright 2014 The Kythe Authors. All rights reserved.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *   http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  /// \file
    17  /// \brief Functions to convert gcc command lines to clang command lines.
    18  ///
    19  /// Utilities in this file convert from arguments to the gcc frontend to
    20  /// arguments to the clang frontend. They are useful for building tools that act
    21  /// on compilation logs that originally invoked gcc.
    22  ///
    23  /// Terminology:
    24  ///
    25  /// 'argc' and 'argv' have the same meaning as in
    26  ///     `int main(int argc, char* argv[]);`
    27  /// i.e. `argv[]` contains `argc + 1` elements, where `argv[0]` is the path
    28  /// to the program, and `argv[argc]` (the last element) is a `NULL`
    29  /// sentinel.
    30  ///
    31  /// We use the term 'command line' for the `std::vector`
    32  ///     `{ argv[0], argv[1], ..., argv[argc - 1] }`
    33  /// i.e. `argv[]` minus the trailing `NULL`.
    34  ///
    35  /// We use the term 'args' for the `std::vector`
    36  ///     `{ argv[1], argv[2], ..., argv[argc - 1] }`
    37  /// i.e. the command line minus `argv[0]`.
    38  ///
    39  //===----------------------------------------------------------------------===//
    40  
    41  // This file uses the Clang style conventions.
    42  
    43  #ifndef LLVM_CLANG_LIB_DRIVER_COMMANDLINE_UTILS_H
    44  #define LLVM_CLANG_LIB_DRIVER_COMMANDLINE_UTILS_H
    45  
    46  #include <string>
    47  #include <vector>
    48  
    49  namespace kythe {
    50  namespace common {
    51  
    52  // Returns true iff a C++ source file appears on the given command
    53  // line or args.  This doesn't care whether the input contains argv[0]
    54  // or not.
    55  bool HasCxxInputInCommandLineOrArgs(
    56      const std::vector<std::string>& command_line_or_args);
    57  
    58  // Converts GCC's arguments to Clang's arguments by dropping GCC args that
    59  // Clang doesn't understand.
    60  std::vector<std::string> GCCArgsToClangArgs(
    61      const std::vector<std::string>& gcc_args);
    62  
    63  // Converts GCC's arguments to Clang's arguments by dropping GCC args that
    64  // Clang doesn't understand or that are not supported in -fsyntax-only mode
    65  // and adding -fsyntax-only. The return value is guaranteed to contain exactly
    66  // one -fsyntax-only flag.
    67  std::vector<std::string> GCCArgsToClangSyntaxOnlyArgs(
    68      const std::vector<std::string>& gcc_args);
    69  
    70  // Converts GCC's arguments to Clang's arguments by dropping GCC args that
    71  // Clang doesn't understand or that are not supported in --analyze mode
    72  // and adding --analyze. The return value is guaranteed to contain exactly
    73  // one --analyze flag.
    74  std::vector<std::string> GCCArgsToClangAnalyzeArgs(
    75      const std::vector<std::string>& gcc_args);
    76  
    77  // Adds -fsyntax-only to the args, and removes args incompatible with
    78  // -fsyntax-only.  The return value is guaranteed to contain exactly
    79  // one -fsyntax-only flag.
    80  std::vector<std::string> AdjustClangArgsForSyntaxOnly(
    81      const std::vector<std::string>& clang_args);
    82  
    83  // Adds --analyze to the args, and removes args incompatible with
    84  // --analyze.  The return value is guaranteed to contain exactly
    85  // one --analyze flag.
    86  std::vector<std::string> AdjustClangArgsForAnalyze(
    87      const std::vector<std::string>& clang_args);
    88  
    89  // Converts Clang's arguments to GCC's arguments by dropping Clang args that
    90  // GCC doesn't understand.
    91  std::vector<std::string> ClangArgsToGCCArgs(
    92      const std::vector<std::string>& clang_args);
    93  
    94  // Removes and adjusts the flags to be valid for compiling with
    95  // AddressSanitizer.
    96  std::vector<std::string> AdjustClangArgsForAddressSanitizer(
    97      const std::vector<std::string>& input);
    98  
    99  // Converts a std::string std::vector representing a command line into a C
   100  // string std::vector representing the argv (including the trailing NULL).
   101  // Note that the C strings in the result point into the argument std::vector.
   102  // That argument must live and remain unchanged as long as the return
   103  // std::vector lives.
   104  //
   105  // Note that the result std::vector contains char* rather than const char*,
   106  // in order to work with legacy C-style APIs.  It's the caller's responsibility
   107  // not to modify the contents of the C-strings.
   108  std::vector<char*> CommandLineToArgv(
   109      const std::vector<std::string>& command_line);
   110  
   111  // `CommandLineToArgv` should not be used with temporaries.
   112  void CommandLineToArgv(const std::vector<std::string>&&) = delete;
   113  
   114  // Set of possible actions to be performed by the compiler driver.
   115  enum DriverAction {
   116    ASSEMBLY,
   117    CXX_COMPILE,
   118    C_COMPILE,
   119    FORTRAN_COMPILE,
   120    GO_COMPILE,
   121    LINK,
   122    UNKNOWN
   123  };
   124  
   125  // Decides what will the driver do based on the inputs found on the command
   126  // line.
   127  DriverAction DetermineDriverAction(const std::vector<std::string>& args);
   128  
   129  }  // namespace common
   130  }  // namespace kythe
   131  
   132  #endif  // LLVM_CLANG_LIB_DRIVER_COMMANDLINE_UTILS_H