xref: /freebsd-src/contrib/llvm-project/lldb/source/Plugins/ExpressionParser/Clang/CppModuleConfiguration.h (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
19dba64beSDimitry Andric //===-- CppModuleConfiguration.h --------------------------------*- C++ -*-===//
29dba64beSDimitry Andric //
39dba64beSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
49dba64beSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
59dba64beSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
69dba64beSDimitry Andric //
79dba64beSDimitry Andric //===----------------------------------------------------------------------===//
89dba64beSDimitry Andric 
95ffd83dbSDimitry Andric #ifndef LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CPPMODULECONFIGURATION_H
105ffd83dbSDimitry Andric #define LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CPPMODULECONFIGURATION_H
119dba64beSDimitry Andric 
12*06c3fb27SDimitry Andric #include <lldb/Utility/FileSpecList.h>
139dba64beSDimitry Andric #include <llvm/Support/Regex.h>
149dba64beSDimitry Andric 
159dba64beSDimitry Andric namespace lldb_private {
169dba64beSDimitry Andric 
179dba64beSDimitry Andric /// A Clang configuration when importing C++ modules.
189dba64beSDimitry Andric ///
19349cc55cSDimitry Andric /// This class computes a list of include paths and module names that can be
20349cc55cSDimitry Andric /// imported given a list of source files. Currently only used when importing
21349cc55cSDimitry Andric /// the 'std' module and its dependencies.
229dba64beSDimitry Andric class CppModuleConfiguration {
239dba64beSDimitry Andric   /// Utility class for a path that can only be set once.
249dba64beSDimitry Andric   class SetOncePath {
259dba64beSDimitry Andric     std::string m_path;
269dba64beSDimitry Andric     bool m_valid = false;
279dba64beSDimitry Andric     /// True iff this path hasn't been set yet.
289dba64beSDimitry Andric     bool m_first = true;
299dba64beSDimitry Andric 
309dba64beSDimitry Andric   public:
319dba64beSDimitry Andric     /// Try setting the path. Returns true if the path was set and false if
329dba64beSDimitry Andric     /// the path was already set.
33bdd1243dSDimitry Andric     [[nodiscard]] bool TrySet(llvm::StringRef path);
349dba64beSDimitry Andric     /// Return the path if there is one.
Get()35e8d8bef9SDimitry Andric     llvm::StringRef Get() const {
369dba64beSDimitry Andric       assert(m_valid && "Called Get() on an invalid SetOncePath?");
379dba64beSDimitry Andric       return m_path;
389dba64beSDimitry Andric     }
399dba64beSDimitry Andric     /// Returns true iff this path was set exactly once so far.
Valid()409dba64beSDimitry Andric     bool Valid() const { return m_valid; }
419dba64beSDimitry Andric   };
429dba64beSDimitry Andric 
439dba64beSDimitry Andric   /// If valid, the include path used for the std module.
449dba64beSDimitry Andric   SetOncePath m_std_inc;
454824e7fdSDimitry Andric   /// If valid, the per-target include path used for the std module.
464824e7fdSDimitry Andric   /// This is an optional path only required on some systems.
474824e7fdSDimitry Andric   SetOncePath m_std_target_inc;
489dba64beSDimitry Andric   /// If valid, the include path to the C library (e.g. /usr/include).
499dba64beSDimitry Andric   SetOncePath m_c_inc;
504824e7fdSDimitry Andric   /// If valid, the include path to target-specific C library files
514824e7fdSDimitry Andric   /// (e.g. /usr/include/x86_64-linux-gnu).
524824e7fdSDimitry Andric   /// This is an optional path only required on some systems.
534824e7fdSDimitry Andric   SetOncePath m_c_target_inc;
549dba64beSDimitry Andric   /// The Clang resource include path for this configuration.
559dba64beSDimitry Andric   std::string m_resource_inc;
569dba64beSDimitry Andric 
579dba64beSDimitry Andric   std::vector<std::string> m_include_dirs;
589dba64beSDimitry Andric   std::vector<std::string> m_imported_modules;
599dba64beSDimitry Andric 
609dba64beSDimitry Andric   /// Analyze a given source file to build the current configuration.
619dba64beSDimitry Andric   /// Returns false iff there was a fatal error that makes analyzing any
629dba64beSDimitry Andric   /// further files pointless as the configuration is now invalid.
634824e7fdSDimitry Andric   bool analyzeFile(const FileSpec &f, const llvm::Triple &triple);
649dba64beSDimitry Andric 
659dba64beSDimitry Andric public:
665ffd83dbSDimitry Andric   /// Creates a configuration by analyzing the given list of used source files.
674824e7fdSDimitry Andric   /// The triple (if valid) is used to search for target-specific include paths.
684824e7fdSDimitry Andric   explicit CppModuleConfiguration(const FileSpecList &support_files,
694824e7fdSDimitry Andric                                   const llvm::Triple &triple);
709dba64beSDimitry Andric   /// Creates an empty and invalid configuration.
71fe6060f1SDimitry Andric   CppModuleConfiguration() = default;
729dba64beSDimitry Andric 
739dba64beSDimitry Andric   /// Returns true iff this is a valid configuration that can be used to
749dba64beSDimitry Andric   /// load and compile modules.
759dba64beSDimitry Andric   bool hasValidConfig();
769dba64beSDimitry Andric 
779dba64beSDimitry Andric   /// Returns a list of include directories that should be used when using this
789dba64beSDimitry Andric   /// configuration (e.g. {"/usr/include", "/usr/include/c++/v1"}).
GetIncludeDirs()799dba64beSDimitry Andric   llvm::ArrayRef<std::string> GetIncludeDirs() const { return m_include_dirs; }
809dba64beSDimitry Andric 
819dba64beSDimitry Andric   /// Returns a list of (top level) modules that should be imported when using
829dba64beSDimitry Andric   /// this configuration (e.g. {"std"}).
GetImportedModules()839dba64beSDimitry Andric   llvm::ArrayRef<std::string> GetImportedModules() const {
849dba64beSDimitry Andric     return m_imported_modules;
859dba64beSDimitry Andric   }
869dba64beSDimitry Andric };
879dba64beSDimitry Andric 
889dba64beSDimitry Andric } // namespace lldb_private
899dba64beSDimitry Andric 
909dba64beSDimitry Andric #endif
91