clang_invocation.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #ifndef CARBON_TOOLCHAIN_BASE_CLANG_INVOCATION_H_
  5. #define CARBON_TOOLCHAIN_BASE_CLANG_INVOCATION_H_
  6. #include <string>
  7. #include "clang/Basic/Diagnostic.h"
  8. #include "clang/Frontend/CompilerInvocation.h"
  9. #include "llvm/ADT/ArrayRef.h"
  10. #include "llvm/ADT/IntrusiveRefCntPtr.h"
  11. #include "llvm/Support/VirtualFileSystem.h"
  12. #include "toolchain/base/install_paths.h"
  13. #include "toolchain/diagnostics/emitter.h"
  14. namespace Carbon {
  15. // Converts diagnostics from the Clang driver to Carbon diagnostics.
  16. class ClangDriverDiagnosticConsumer : public clang::DiagnosticConsumer {
  17. public:
  18. // Creates an instance with the location that triggers calling Clang.
  19. // `context` must not be null.
  20. explicit ClangDriverDiagnosticConsumer(Diagnostics::NoLocEmitter* emitter)
  21. : emitter_(emitter) {}
  22. // Generates a Carbon warning for each Clang warning and a Carbon error for
  23. // each Clang error or fatal.
  24. auto HandleDiagnostic(clang::DiagnosticsEngine::Level diag_level,
  25. const clang::Diagnostic& info) -> void override;
  26. private:
  27. // Diagnostic emitter. Note that driver diagnostics don't have meaningful
  28. // locations attached.
  29. Diagnostics::NoLocEmitter* emitter_;
  30. };
  31. // Builds and returns a clang `CompilerInvocation` to use when building code for
  32. // interop, from a list of clang driver arguments. Emits diagnostics to
  33. // `consumer` if the arguments are invalid.
  34. auto BuildClangInvocation(Diagnostics::Consumer& consumer,
  35. llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs,
  36. const InstallPaths& install_paths,
  37. llvm::StringRef target_str,
  38. llvm::ArrayRef<llvm::StringRef> extra_args = {})
  39. -> std::unique_ptr<clang::CompilerInvocation>;
  40. // Appends the default Clang command line arguments used when building a
  41. // Carbon-compatible Clang invocation.
  42. //
  43. // Where possible, code should use `BuildClangInvocation` above. However, when
  44. // invoking Clang directly, this can be used to get the core compatible flags.
  45. auto AppendDefaultClangArgs(const InstallPaths& install_paths,
  46. llvm::StringRef target_str,
  47. llvm::SmallVectorImpl<std::string>& args) -> void;
  48. } // namespace Carbon
  49. #endif // CARBON_TOOLCHAIN_BASE_CLANG_INVOCATION_H_