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