xref: /freebsd-src/contrib/llvm-project/lldb/source/Plugins/ExpressionParser/Clang/CppModuleConfiguration.h (revision 9dba64be9536c28e4800e06512b7f29b43ade345)
1*9dba64beSDimitry Andric //===-- CppModuleConfiguration.h --------------------------------*- C++ -*-===//
2*9dba64beSDimitry Andric //
3*9dba64beSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*9dba64beSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*9dba64beSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*9dba64beSDimitry Andric //
7*9dba64beSDimitry Andric //===----------------------------------------------------------------------===//
8*9dba64beSDimitry Andric 
9*9dba64beSDimitry Andric #ifndef liblldb_CppModuleConfiguration_h_
10*9dba64beSDimitry Andric #define liblldb_CppModuleConfiguration_h_
11*9dba64beSDimitry Andric 
12*9dba64beSDimitry Andric #include <lldb/Core/FileSpecList.h>
13*9dba64beSDimitry Andric #include <llvm/Support/Regex.h>
14*9dba64beSDimitry Andric 
15*9dba64beSDimitry Andric namespace lldb_private {
16*9dba64beSDimitry Andric 
17*9dba64beSDimitry Andric /// A Clang configuration when importing C++ modules.
18*9dba64beSDimitry Andric ///
19*9dba64beSDimitry Andric /// Includes a list of include paths that should be used when importing
20*9dba64beSDimitry Andric /// and a list of modules that can be imported. Currently only used when
21*9dba64beSDimitry Andric /// importing the 'std' module and its dependencies.
22*9dba64beSDimitry Andric class CppModuleConfiguration {
23*9dba64beSDimitry Andric   /// Utility class for a path that can only be set once.
24*9dba64beSDimitry Andric   class SetOncePath {
25*9dba64beSDimitry Andric     std::string m_path;
26*9dba64beSDimitry Andric     bool m_valid = false;
27*9dba64beSDimitry Andric     /// True iff this path hasn't been set yet.
28*9dba64beSDimitry Andric     bool m_first = true;
29*9dba64beSDimitry Andric 
30*9dba64beSDimitry Andric   public:
31*9dba64beSDimitry Andric     /// Try setting the path. Returns true if the path was set and false if
32*9dba64beSDimitry Andric     /// the path was already set.
33*9dba64beSDimitry Andric     LLVM_NODISCARD bool TrySet(llvm::StringRef path);
34*9dba64beSDimitry Andric     /// Return the path if there is one.
35*9dba64beSDimitry Andric     std::string Get() const {
36*9dba64beSDimitry Andric       assert(m_valid && "Called Get() on an invalid SetOncePath?");
37*9dba64beSDimitry Andric       return m_path;
38*9dba64beSDimitry Andric     }
39*9dba64beSDimitry Andric     /// Returns true iff this path was set exactly once so far.
40*9dba64beSDimitry Andric     bool Valid() const { return m_valid; }
41*9dba64beSDimitry Andric   };
42*9dba64beSDimitry Andric 
43*9dba64beSDimitry Andric   /// If valid, the include path used for the std module.
44*9dba64beSDimitry Andric   SetOncePath m_std_inc;
45*9dba64beSDimitry Andric   /// If valid, the include path to the C library (e.g. /usr/include).
46*9dba64beSDimitry Andric   SetOncePath m_c_inc;
47*9dba64beSDimitry Andric   /// The Clang resource include path for this configuration.
48*9dba64beSDimitry Andric   std::string m_resource_inc;
49*9dba64beSDimitry Andric 
50*9dba64beSDimitry Andric   std::vector<std::string> m_include_dirs;
51*9dba64beSDimitry Andric   std::vector<std::string> m_imported_modules;
52*9dba64beSDimitry Andric 
53*9dba64beSDimitry Andric   /// Analyze a given source file to build the current configuration.
54*9dba64beSDimitry Andric   /// Returns false iff there was a fatal error that makes analyzing any
55*9dba64beSDimitry Andric   /// further files pointless as the configuration is now invalid.
56*9dba64beSDimitry Andric   bool analyzeFile(const FileSpec &f);
57*9dba64beSDimitry Andric 
58*9dba64beSDimitry Andric public:
59*9dba64beSDimitry Andric   /// Creates a configuraiton by analyzing the given list of used source files.
60*9dba64beSDimitry Andric   ///
61*9dba64beSDimitry Andric   /// Currently only looks at the used paths and doesn't actually access the
62*9dba64beSDimitry Andric   /// files on the disk.
63*9dba64beSDimitry Andric   explicit CppModuleConfiguration(const FileSpecList &support_files);
64*9dba64beSDimitry Andric   /// Creates an empty and invalid configuration.
65*9dba64beSDimitry Andric   CppModuleConfiguration() {}
66*9dba64beSDimitry Andric 
67*9dba64beSDimitry Andric   /// Returns true iff this is a valid configuration that can be used to
68*9dba64beSDimitry Andric   /// load and compile modules.
69*9dba64beSDimitry Andric   bool hasValidConfig();
70*9dba64beSDimitry Andric 
71*9dba64beSDimitry Andric   /// Returns a list of include directories that should be used when using this
72*9dba64beSDimitry Andric   /// configuration (e.g. {"/usr/include", "/usr/include/c++/v1"}).
73*9dba64beSDimitry Andric   llvm::ArrayRef<std::string> GetIncludeDirs() const { return m_include_dirs; }
74*9dba64beSDimitry Andric 
75*9dba64beSDimitry Andric   /// Returns a list of (top level) modules that should be imported when using
76*9dba64beSDimitry Andric   /// this configuration (e.g. {"std"}).
77*9dba64beSDimitry Andric   llvm::ArrayRef<std::string> GetImportedModules() const {
78*9dba64beSDimitry Andric     return m_imported_modules;
79*9dba64beSDimitry Andric   }
80*9dba64beSDimitry Andric };
81*9dba64beSDimitry Andric 
82*9dba64beSDimitry Andric } // namespace lldb_private
83*9dba64beSDimitry Andric 
84*9dba64beSDimitry Andric #endif
85