xref: /llvm-project/lldb/source/Plugins/ExpressionParser/Clang/CppModuleConfiguration.h (revision 917b3a7e62063398d2cbc4f8fe56feb68b0fae4f)
19379d19fSRaphael Isemann //===-- CppModuleConfiguration.h --------------------------------*- C++ -*-===//
29379d19fSRaphael Isemann //
39379d19fSRaphael Isemann // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
49379d19fSRaphael Isemann // See https://llvm.org/LICENSE.txt for license information.
59379d19fSRaphael Isemann // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
69379d19fSRaphael Isemann //
79379d19fSRaphael Isemann //===----------------------------------------------------------------------===//
89379d19fSRaphael Isemann 
9cdc514e4SJonas Devlieghere #ifndef LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CPPMODULECONFIGURATION_H
10cdc514e4SJonas Devlieghere #define LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CPPMODULECONFIGURATION_H
119379d19fSRaphael Isemann 
12*917b3a7eSJonas Devlieghere #include <lldb/Utility/FileSpecList.h>
139379d19fSRaphael Isemann #include <llvm/Support/Regex.h>
149379d19fSRaphael Isemann 
159379d19fSRaphael Isemann namespace lldb_private {
169379d19fSRaphael Isemann 
179379d19fSRaphael Isemann /// A Clang configuration when importing C++ modules.
189379d19fSRaphael Isemann ///
19f5c65be5SRaphael Isemann /// This class computes a list of include paths and module names that can be
20f5c65be5SRaphael Isemann /// imported given a list of source files. Currently only used when importing
21f5c65be5SRaphael Isemann /// the 'std' module and its dependencies.
229379d19fSRaphael Isemann class CppModuleConfiguration {
239379d19fSRaphael Isemann   /// Utility class for a path that can only be set once.
249379d19fSRaphael Isemann   class SetOncePath {
259379d19fSRaphael Isemann     std::string m_path;
269379d19fSRaphael Isemann     bool m_valid = false;
279379d19fSRaphael Isemann     /// True iff this path hasn't been set yet.
289379d19fSRaphael Isemann     bool m_first = true;
299379d19fSRaphael Isemann 
309379d19fSRaphael Isemann   public:
319379d19fSRaphael Isemann     /// Try setting the path. Returns true if the path was set and false if
329379d19fSRaphael Isemann     /// the path was already set.
335146f84fSFangrui Song     [[nodiscard]] bool TrySet(llvm::StringRef path);
349379d19fSRaphael Isemann     /// Return the path if there is one.
Get()3599b7b41eSRaphael Isemann     llvm::StringRef Get() const {
369379d19fSRaphael Isemann       assert(m_valid && "Called Get() on an invalid SetOncePath?");
379379d19fSRaphael Isemann       return m_path;
389379d19fSRaphael Isemann     }
399379d19fSRaphael Isemann     /// Returns true iff this path was set exactly once so far.
Valid()409379d19fSRaphael Isemann     bool Valid() const { return m_valid; }
419379d19fSRaphael Isemann   };
429379d19fSRaphael Isemann 
439379d19fSRaphael Isemann   /// If valid, the include path used for the std module.
449379d19fSRaphael Isemann   SetOncePath m_std_inc;
451aab5e65SPavel Kosov   /// If valid, the per-target include path used for the std module.
461aab5e65SPavel Kosov   /// This is an optional path only required on some systems.
471aab5e65SPavel Kosov   SetOncePath m_std_target_inc;
489379d19fSRaphael Isemann   /// If valid, the include path to the C library (e.g. /usr/include).
499379d19fSRaphael Isemann   SetOncePath m_c_inc;
501aab5e65SPavel Kosov   /// If valid, the include path to target-specific C library files
511aab5e65SPavel Kosov   /// (e.g. /usr/include/x86_64-linux-gnu).
521aab5e65SPavel Kosov   /// This is an optional path only required on some systems.
531aab5e65SPavel Kosov   SetOncePath m_c_target_inc;
549379d19fSRaphael Isemann   /// The Clang resource include path for this configuration.
559379d19fSRaphael Isemann   std::string m_resource_inc;
569379d19fSRaphael Isemann 
579379d19fSRaphael Isemann   std::vector<std::string> m_include_dirs;
589379d19fSRaphael Isemann   std::vector<std::string> m_imported_modules;
599379d19fSRaphael Isemann 
609379d19fSRaphael Isemann   /// Analyze a given source file to build the current configuration.
619379d19fSRaphael Isemann   /// Returns false iff there was a fatal error that makes analyzing any
629379d19fSRaphael Isemann   /// further files pointless as the configuration is now invalid.
631aab5e65SPavel Kosov   bool analyzeFile(const FileSpec &f, const llvm::Triple &triple);
649379d19fSRaphael Isemann 
659379d19fSRaphael Isemann public:
66e9264b74SKazuaki Ishizaki   /// Creates a configuration by analyzing the given list of used source files.
671aab5e65SPavel Kosov   /// The triple (if valid) is used to search for target-specific include paths.
681aab5e65SPavel Kosov   explicit CppModuleConfiguration(const FileSpecList &support_files,
691aab5e65SPavel Kosov                                   const llvm::Triple &triple);
709379d19fSRaphael Isemann   /// Creates an empty and invalid configuration.
71fd2433e1SJonas Devlieghere   CppModuleConfiguration() = default;
729379d19fSRaphael Isemann 
739379d19fSRaphael Isemann   /// Returns true iff this is a valid configuration that can be used to
749379d19fSRaphael Isemann   /// load and compile modules.
759379d19fSRaphael Isemann   bool hasValidConfig();
769379d19fSRaphael Isemann 
779379d19fSRaphael Isemann   /// Returns a list of include directories that should be used when using this
789379d19fSRaphael Isemann   /// configuration (e.g. {"/usr/include", "/usr/include/c++/v1"}).
GetIncludeDirs()799379d19fSRaphael Isemann   llvm::ArrayRef<std::string> GetIncludeDirs() const { return m_include_dirs; }
809379d19fSRaphael Isemann 
819379d19fSRaphael Isemann   /// Returns a list of (top level) modules that should be imported when using
829379d19fSRaphael Isemann   /// this configuration (e.g. {"std"}).
GetImportedModules()839379d19fSRaphael Isemann   llvm::ArrayRef<std::string> GetImportedModules() const {
849379d19fSRaphael Isemann     return m_imported_modules;
859379d19fSRaphael Isemann   }
869379d19fSRaphael Isemann };
879379d19fSRaphael Isemann 
889379d19fSRaphael Isemann } // namespace lldb_private
899379d19fSRaphael Isemann 
909379d19fSRaphael Isemann #endif
91