Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:42:51

0001 //===-- CompileUnit.h -------------------------------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLDB_SYMBOL_COMPILEUNIT_H
0010 #define LLDB_SYMBOL_COMPILEUNIT_H
0011 
0012 #include "lldb/Core/ModuleChild.h"
0013 #include "lldb/Core/SourceLocationSpec.h"
0014 #include "lldb/Symbol/DebugMacros.h"
0015 #include "lldb/Symbol/Function.h"
0016 #include "lldb/Symbol/LineTable.h"
0017 #include "lldb/Symbol/SourceModule.h"
0018 #include "lldb/Utility/FileSpecList.h"
0019 #include "lldb/Utility/Stream.h"
0020 #include "lldb/Utility/UserID.h"
0021 #include "lldb/lldb-enumerations.h"
0022 #include "lldb/lldb-forward.h"
0023 
0024 #include "llvm/ADT/DenseMap.h"
0025 #include "llvm/ADT/DenseSet.h"
0026 
0027 namespace lldb_private {
0028 
0029 /// \class CompileUnit CompileUnit.h "lldb/Symbol/CompileUnit.h"
0030 /// A class that describes a compilation unit.
0031 ///
0032 /// A representation of a compilation unit, or compiled source file.
0033 /// The UserID of the compile unit is specified by the SymbolFile plug-in and
0034 /// can have any value as long as the value is unique within the Module that
0035 /// owns this compile units.
0036 ///
0037 /// Each compile unit has a list of functions, global and static variables,
0038 /// support file list (include files and inlined source files), and a line
0039 /// table.
0040 class CompileUnit : public std::enable_shared_from_this<CompileUnit>,
0041                     public ModuleChild,
0042                     public UserID,
0043                     public SymbolContextScope {
0044 public:
0045   /// Construct with a module, path, UID and language.
0046   ///
0047   /// Initialize the compile unit given the owning \a module, a path to
0048   /// convert into a FileSpec, the SymbolFile plug-in supplied \a uid, and the
0049   /// source language type.
0050   ///
0051   /// \param[in] module_sp
0052   ///     The parent module that owns this compile unit. This value
0053   ///     must be a valid pointer value.
0054   ///
0055   /// \param[in] user_data
0056   ///     User data where the SymbolFile parser can store data.
0057   ///
0058   /// \param[in] pathname
0059   ///     The path to the source file for this compile unit.
0060   ///
0061   /// \param[in] uid
0062   ///     The user ID of the compile unit. This value is supplied by
0063   ///     the SymbolFile plug-in and should be a value that allows
0064   ///     the SymbolFile plug-in to easily locate and parse additional
0065   ///     information for the compile unit.
0066   ///
0067   /// \param[in] language
0068   ///     A language enumeration type that describes the main language
0069   ///     of this compile unit.
0070   ///
0071   /// \param[in] is_optimized
0072   ///     A value that can initialized with eLazyBoolYes, eLazyBoolNo
0073   ///     or eLazyBoolCalculate. If set to eLazyBoolCalculate, then
0074   ///     an extra call into SymbolVendor will be made to calculate if
0075   ///     the compile unit is optimized will be made when
0076   ///     CompileUnit::GetIsOptimized() is called.
0077   ///
0078   /// \see lldb::LanguageType
0079   CompileUnit(const lldb::ModuleSP &module_sp, void *user_data,
0080               const char *pathname, lldb::user_id_t uid,
0081               lldb::LanguageType language, lldb_private::LazyBool is_optimized);
0082 
0083   /// Construct with a module, file spec, UID and language.
0084   ///
0085   /// Initialize the compile unit given the owning \a module, a path to
0086   /// convert into a FileSpec, the SymbolFile plug-in supplied \a uid, and the
0087   /// source language type.
0088   ///
0089   /// \param[in] module_sp
0090   ///     The parent module that owns this compile unit. This value
0091   ///     must be a valid pointer value.
0092   ///
0093   /// \param[in] user_data
0094   ///     User data where the SymbolFile parser can store data.
0095   ///
0096   /// \param[in] support_file_sp
0097   ///     The file specification for the source file of this compile
0098   ///     unit.
0099   ///
0100   /// \param[in] uid
0101   ///     The user ID of the compile unit. This value is supplied by
0102   ///     the SymbolFile plug-in and should be a value that allows
0103   ///     the plug-in to easily locate and parse
0104   ///     additional information for the compile unit.
0105   ///
0106   /// \param[in] language
0107   ///     A language enumeration type that describes the main language
0108   ///     of this compile unit.
0109   ///
0110   /// \param[in] is_optimized
0111   ///     A value that can initialized with eLazyBoolYes, eLazyBoolNo
0112   ///     or eLazyBoolCalculate. If set to eLazyBoolCalculate, then
0113   ///     an extra call into SymbolVendor will be made to calculate if
0114   ///     the compile unit is optimized will be made when
0115   ///     CompileUnit::GetIsOptimized() is called.
0116   ///
0117   /// \param[in] support_files
0118   ///     An rvalue list of already parsed support files.
0119   /// \see lldb::LanguageType
0120   CompileUnit(const lldb::ModuleSP &module_sp, void *user_data,
0121               lldb::SupportFileSP support_file_sp, lldb::user_id_t uid,
0122               lldb::LanguageType language, lldb_private::LazyBool is_optimized,
0123               SupportFileList &&support_files = {});
0124 
0125   /// Add a function to this compile unit.
0126   ///
0127   /// Typically called by the SymbolFile plug-ins as they partially parse the
0128   /// debug information.
0129   ///
0130   /// \param[in] function_sp
0131   ///     A shared pointer to the Function object.
0132   void AddFunction(lldb::FunctionSP &function_sp);
0133 
0134   /// \copydoc SymbolContextScope::CalculateSymbolContext(SymbolContext*)
0135   ///
0136   /// \see SymbolContextScope
0137   void CalculateSymbolContext(SymbolContext *sc) override;
0138 
0139   lldb::ModuleSP CalculateSymbolContextModule() override;
0140 
0141   CompileUnit *CalculateSymbolContextCompileUnit() override;
0142 
0143   /// \copydoc SymbolContextScope::DumpSymbolContext(Stream*)
0144   ///
0145   /// \see SymbolContextScope
0146   void DumpSymbolContext(Stream *s) override;
0147 
0148   lldb::LanguageType GetLanguage();
0149 
0150   void SetLanguage(lldb::LanguageType language) {
0151     m_flags.Set(flagsParsedLanguage);
0152     m_language = language;
0153   }
0154 
0155   void GetDescription(Stream *s, lldb::DescriptionLevel level) const;
0156 
0157   /// Apply a lambda to each function in this compile unit.
0158   ///
0159   /// This provides raw access to the function shared pointer list and will not
0160   /// cause the SymbolFile plug-in to parse any unparsed functions.
0161   ///
0162   /// \note Prefer using FindFunctionByUID over this if possible.
0163   ///
0164   /// \param[in] lambda
0165   ///     The lambda that should be applied to every function. The lambda can
0166   ///     return true if the iteration should be aborted earlier.
0167   void ForeachFunction(
0168       llvm::function_ref<bool(const lldb::FunctionSP &)> lambda) const;
0169 
0170   /// Find a function in the compile unit based on the predicate matching_lambda
0171   ///
0172   /// \param[in] matching_lambda
0173   ///     A predicate that will be used within FindFunction to evaluate each
0174   ///     FunctionSP in m_functions_by_uid. When the predicate returns true
0175   ///     FindFunction will return the corresponding FunctionSP.
0176   ///
0177   /// \return
0178   ///   The first FunctionSP that the matching_lambda prediate returns true for.
0179   lldb::FunctionSP FindFunction(
0180       llvm::function_ref<bool(const lldb::FunctionSP &)> matching_lambda);
0181 
0182   /// Dump the compile unit contents to the stream \a s.
0183   ///
0184   /// \param[in] s
0185   ///     The stream to which to dump the object description.
0186   ///
0187   /// \param[in] show_context
0188   ///     If \b true, variables will dump their symbol context
0189   ///     information.
0190   void Dump(Stream *s, bool show_context) const;
0191 
0192   /// Find the line entry by line and optional inlined file spec.
0193   ///
0194   /// Finds the first line entry that has an index greater than \a start_idx
0195   /// that matches \a line. If \a file_spec_ptr is NULL, then the search
0196   /// matches line entries whose file matches the file for the compile unit.
0197   /// If \a file_spec_ptr is not NULL, line entries must match the specified
0198   /// file spec (for inlined line table entries).
0199   ///
0200   /// Multiple calls to this function can find all entries that match a given
0201   /// file and line by starting with \a start_idx equal to zero, and calling
0202   /// this function back with the return value + 1.
0203   ///
0204   /// \param[in] start_idx
0205   ///     The zero based index at which to start looking for matches.
0206   ///
0207   /// \param[in] line
0208   ///     The line number to search for.
0209   ///
0210   /// \param[in] file_spec_ptr
0211   ///     If non-NULL search for entries that match this file spec,
0212   ///     else if NULL, search for line entries that match the compile
0213   ///     unit file.
0214   ///
0215   /// \param[in] exact
0216   ///     If \b true match only if there is a line table entry for this line
0217   ///     number.
0218   ///     If \b false, find the line table entry equal to or after this line
0219   ///     number.
0220   ///
0221   /// \param[out] line_entry
0222   ///     If non-NULL, a copy of the line entry that was found.
0223   ///
0224   /// \return
0225   ///     The zero based index of a matching line entry, or UINT32_MAX
0226   ///     if no matching line entry is found.
0227   uint32_t FindLineEntry(uint32_t start_idx, uint32_t line,
0228                          const FileSpec *file_spec_ptr, bool exact,
0229                          LineEntry *line_entry);
0230 
0231   /// Return the primary source spec associated with this compile unit.
0232   const FileSpec &GetPrimaryFile() const {
0233     return m_primary_support_file_sp->GetSpecOnly();
0234   }
0235 
0236   /// Return the primary source file associated with this compile unit.
0237   lldb::SupportFileSP GetPrimarySupportFile() const {
0238     return m_primary_support_file_sp;
0239   }
0240 
0241   /// Get the line table for the compile unit.
0242   ///
0243   /// Called by clients and the SymbolFile plug-in. The SymbolFile plug-ins
0244   /// use this function to determine if the line table has be parsed yet.
0245   /// Clients use this function to get the line table from a compile unit.
0246   ///
0247   /// \return
0248   ///     The line table object pointer, or NULL if this line table
0249   ///     hasn't been parsed yet.
0250   LineTable *GetLineTable();
0251 
0252   DebugMacros *GetDebugMacros();
0253 
0254   /// Apply a lambda to each external lldb::Module referenced by this
0255   /// compilation unit. Recursively also descends into the referenced external
0256   /// modules of any encountered compilation unit.
0257   ///
0258   /// \param visited_symbol_files
0259   ///     A set of SymbolFiles that were already visited to avoid
0260   ///     visiting one file more than once.
0261   ///
0262   /// \param[in] lambda
0263   ///     The lambda that should be applied to every function. The lambda can
0264   ///     return true if the iteration should be aborted earlier.
0265   ///
0266   /// \return
0267   ///     If the lambda early-exited, this function returns true to
0268   ///     propagate the early exit.
0269   virtual bool ForEachExternalModule(
0270       llvm::DenseSet<lldb_private::SymbolFile *> &visited_symbol_files,
0271       llvm::function_ref<bool(Module &)> lambda);
0272 
0273   /// Get the compile unit's support file list.
0274   ///
0275   /// The support file list is used by the line table, and any objects that
0276   /// have valid Declaration objects.
0277   ///
0278   /// \return
0279   ///     A support file list object.
0280   const SupportFileList &GetSupportFiles();
0281 
0282   /// Used by plugins that parse the support file list.
0283   SupportFileList &GetSupportFileList() {
0284     m_flags.Set(flagsParsedSupportFiles);
0285     return m_support_files;
0286   }
0287 
0288   /// Get the compile unit's imported module list.
0289   ///
0290   /// This reports all the imports that the compile unit made, including the
0291   /// current module.
0292   ///
0293   /// \return
0294   ///     A list of imported modules.
0295   const std::vector<SourceModule> &GetImportedModules();
0296 
0297   /// Get the SymbolFile plug-in user data.
0298   ///
0299   /// SymbolFile plug-ins can store user data to internal state or objects to
0300   /// quickly allow them to parse more information for a given object.
0301   ///
0302   /// \return
0303   ///     The user data stored with the CompileUnit when it was
0304   ///     constructed.
0305   void *GetUserData() const;
0306 
0307   /// Get the variable list for a compile unit.
0308   ///
0309   /// Called by clients to get the variable list for a compile unit. The
0310   /// variable list will contain all global and static variables that were
0311   /// defined at the compile unit level.
0312   ///
0313   /// \param[in] can_create
0314   ///     If \b true, the variable list will be parsed on demand. If
0315   ///     \b false, the current variable list will be returned even
0316   ///     if it contains a NULL VariableList object (typically
0317   ///     called by dumping routines that want to display only what
0318   ///     has currently been parsed).
0319   ///
0320   /// \return
0321   ///     A shared pointer to a variable list, that can contain NULL
0322   ///     VariableList pointer if there are no global or static
0323   ///     variables.
0324   lldb::VariableListSP GetVariableList(bool can_create);
0325 
0326   /// Finds a function by user ID.
0327   ///
0328   /// Typically used by SymbolFile plug-ins when partially parsing the debug
0329   /// information to see if the function has been parsed yet.
0330   ///
0331   /// \param[in] uid
0332   ///     The user ID of the function to find. This value is supplied
0333   ///     by the SymbolFile plug-in and should be a value that
0334   ///     allows the plug-in to easily locate and parse additional
0335   ///     information in the function.
0336   ///
0337   /// \return
0338   ///     A shared pointer to the function object that might contain
0339   ///     a NULL Function pointer.
0340   lldb::FunctionSP FindFunctionByUID(lldb::user_id_t uid);
0341 
0342   /// Set the line table for the compile unit.
0343   ///
0344   /// Called by the SymbolFile plug-in when if first parses the line table and
0345   /// hands ownership of the line table to this object. The compile unit owns
0346   /// the line table object and will delete the object when it is deleted.
0347   ///
0348   /// \param[in] line_table
0349   ///     A line table object pointer that this object now owns.
0350   void SetLineTable(LineTable *line_table);
0351 
0352   void SetDebugMacros(const DebugMacrosSP &debug_macros);
0353 
0354   /// Set accessor for the variable list.
0355   ///
0356   /// Called by the SymbolFile plug-ins after they have parsed the variable
0357   /// lists and are ready to hand ownership of the list over to this object.
0358   ///
0359   /// \param[in] variable_list_sp
0360   ///     A shared pointer to a VariableList.
0361   void SetVariableList(lldb::VariableListSP &variable_list_sp);
0362 
0363   /// Resolve symbol contexts by file and line.
0364   ///
0365   /// Given a file in \a src_location_spec, find all instances and
0366   /// append them to the supplied symbol context list \a sc_list.
0367   ///
0368   /// \param[in] src_location_spec
0369   ///     The \a src_location_spec containing the \a file_spec, the line and the
0370   ///     column of the symbol to look for. Also hold the inlines and
0371   ///     exact_match flags.
0372   ///
0373   ///     If check_inlines is \b true, this function will also match any inline
0374   ///     file and line matches. If \b false, the compile unit's
0375   ///     file specification must match \a file_spec for any matches
0376   ///     to be returned.
0377   ///
0378   ///     If exact_match is \b true, only resolve the context if \a line and \a
0379   ///     column exists in the line table. If \b false, resolve the context to
0380   ///     the closest line greater than \a line in the line table.
0381   ///
0382   /// \param[in] resolve_scope
0383   ///     For each matching line entry, this bitfield indicates what
0384   ///     values within each SymbolContext that gets added to \a
0385   ///     sc_list will be resolved. See the SymbolContext::Scope
0386   ///     enumeration for a list of all available bits that can be
0387   ///     resolved. Only SymbolContext entries that can be resolved
0388   ///     using a LineEntry base address will be able to be resolved.
0389   ///
0390   /// \param[out] sc_list
0391   ///     A SymbolContext list class that will get any matching
0392   ///     entries appended to.
0393   ///
0394   /// \param[in] realpath_prefixes
0395   ///     Paths that start with one of the prefixes in this list will be
0396   ///     realpath'ed to resolve any symlinks.
0397   ///
0398   /// \see enum SymbolContext::Scope
0399   void ResolveSymbolContext(const SourceLocationSpec &src_location_spec,
0400                             lldb::SymbolContextItem resolve_scope,
0401                             SymbolContextList &sc_list,
0402                             RealpathPrefixes *realpath_prefixes = nullptr);
0403 
0404   /// Get whether compiler optimizations were enabled for this compile unit
0405   ///
0406   /// "optimized" means that the debug experience may be difficult for the
0407   /// user to understand.  Variables may not be available when the developer
0408   /// would expect them, stepping through the source lines in the function may
0409   /// appear strange, etc.
0410   ///
0411   /// \return
0412   ///     Returns 'true' if this compile unit was compiled with
0413   ///     optimization.  'false' indicates that either the optimization
0414   ///     is unknown, or this compile unit was built without optimization.
0415   bool GetIsOptimized();
0416 
0417   /// Returns the number of functions in this compile unit
0418   size_t GetNumFunctions() const { return m_functions_by_uid.size(); }
0419 
0420 protected:
0421   /// User data for the SymbolFile parser to store information into.
0422   void *m_user_data;
0423   /// The programming language enumeration value.
0424   lldb::LanguageType m_language;
0425   /// Compile unit flags that help with partial parsing.
0426   Flags m_flags;
0427   /// Maps UIDs to functions.
0428   llvm::DenseMap<lldb::user_id_t, lldb::FunctionSP> m_functions_by_uid;
0429   /// All modules, including the current module, imported by this
0430   /// compile unit.
0431   std::vector<SourceModule> m_imported_modules;
0432   /// The primary file associated with this compile unit.
0433   lldb::SupportFileSP m_primary_support_file_sp;
0434   /// Files associated with this compile unit's line table and declarations.
0435   SupportFileList m_support_files;
0436   /// Line table that will get parsed on demand.
0437   std::unique_ptr<LineTable> m_line_table_up;
0438   /// Debug macros that will get parsed on demand.
0439   DebugMacrosSP m_debug_macros_sp;
0440   /// Global and static variable list that will get parsed on demand.
0441   lldb::VariableListSP m_variables;
0442   /// eLazyBoolYes if this compile unit was compiled with
0443   /// optimization.
0444   lldb_private::LazyBool m_is_optimized;
0445 
0446 private:
0447   enum {
0448     flagsParsedAllFunctions =
0449         (1u << 0), ///< Have we already parsed all our functions
0450     flagsParsedVariables =
0451         (1u << 1), ///< Have we already parsed globals and statics?
0452     flagsParsedSupportFiles = (1u << 2), ///< Have we already parsed the support
0453                                          ///files for this compile unit?
0454     flagsParsedLineTable =
0455         (1u << 3),                   ///< Have we parsed the line table already?
0456     flagsParsedLanguage = (1u << 4), ///< Have we parsed the language already?
0457     flagsParsedImportedModules =
0458         (1u << 5), ///< Have we parsed the imported modules already?
0459     flagsParsedDebugMacros =
0460         (1u << 6) ///< Have we parsed the debug macros already?
0461   };
0462 
0463   CompileUnit(const CompileUnit &) = delete;
0464   const CompileUnit &operator=(const CompileUnit &) = delete;
0465   const char *GetCachedLanguage() const;
0466 };
0467 
0468 } // namespace lldb_private
0469 
0470 #endif // LLDB_SYMBOL_COMPILEUNIT_H