Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 20 additions & 20 deletions lldb/include/lldb/Core/SourceManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include "lldb/Utility/Checksum.h"
#include "lldb/Utility/FileSpec.h"
#include "lldb/Utility/SupportFile.h"
#include "lldb/lldb-defines.h"
#include "lldb/lldb-forward.h"

Expand Down Expand Up @@ -38,8 +39,8 @@ class SourceManager {
const SourceManager::File &rhs);

public:
File(lldb::SupportFileSP support_file_sp, lldb::TargetSP target_sp);
File(lldb::SupportFileSP support_file_sp, lldb::DebuggerSP debugger_sp);
File(SupportFileNSP support_file_nsp, lldb::TargetSP target_sp);
File(SupportFileNSP support_file_nsp, lldb::DebuggerSP debugger_sp);

bool ModificationTimeIsStale() const;
bool PathRemappingIsStale() const;
Expand All @@ -57,9 +58,9 @@ class SourceManager {

bool LineIsValid(uint32_t line);

lldb::SupportFileSP GetSupportFile() const {
assert(m_support_file_sp && "SupportFileSP must always be valid");
return m_support_file_sp;
SupportFileNSP GetSupportFile() const {
assert(m_support_file_nsp && "SupportFileNSP must always be valid");
return m_support_file_nsp;
}

uint32_t GetSourceMapModificationID() const { return m_source_map_mod_id; }
Expand All @@ -80,13 +81,13 @@ class SourceManager {

protected:
/// Set file and update modification time.
void SetSupportFile(lldb::SupportFileSP support_file_sp);
void SetSupportFile(SupportFileNSP support_file_nsp);

bool CalculateLineOffsets(uint32_t line = UINT32_MAX);

/// The support file. If the target has source mappings, this might be
/// different from the original support file passed to the constructor.
lldb::SupportFileSP m_support_file_sp;
SupportFileNSP m_support_file_nsp;

/// Keep track of the on-disk checksum.
Checksum m_checksum;
Expand All @@ -107,9 +108,9 @@ class SourceManager {
lldb::TargetWP m_target_wp;

private:
void CommonInitializer(lldb::SupportFileSP support_file_sp,
void CommonInitializer(SupportFileNSP support_file_nsp,
lldb::TargetSP target_sp);
void CommonInitializerImpl(lldb::SupportFileSP support_file_sp,
void CommonInitializerImpl(SupportFileNSP support_file_nsp,
lldb::TargetSP target_sp);
};

Expand Down Expand Up @@ -156,13 +157,13 @@ class SourceManager {

~SourceManager();

FileSP GetLastFile() { return GetFile(m_last_support_file_sp); }
FileSP GetLastFile() { return GetFile(m_last_support_file_nsp); }
bool AtLastLine(bool reverse) {
return m_last_line == UINT32_MAX || (reverse && m_last_line == 1);
}

size_t DisplaySourceLinesWithLineNumbers(
lldb::SupportFileSP support_file_sp, uint32_t line, uint32_t column,
SupportFileNSP support_file_nsp, uint32_t line, uint32_t column,
uint32_t context_before, uint32_t context_after,
const char *current_line_cstr, Stream *s,
const SymbolContextList *bp_locs = nullptr);
Expand All @@ -176,31 +177,30 @@ class SourceManager {
size_t DisplayMoreWithLineNumbers(Stream *s, uint32_t count, bool reverse,
const SymbolContextList *bp_locs = nullptr);

bool SetDefaultFileAndLine(lldb::SupportFileSP support_file_sp,
uint32_t line);
bool SetDefaultFileAndLine(SupportFileNSP support_file_nsp, uint32_t line);

struct SupportFileAndLine {
lldb::SupportFileSP support_file_sp;
SupportFileNSP support_file_nsp;
uint32_t line;
SupportFileAndLine(lldb::SupportFileSP support_file_sp, uint32_t line)
: support_file_sp(support_file_sp), line(line) {}
SupportFileAndLine(SupportFileNSP support_file_nsp, uint32_t line)
: support_file_nsp(support_file_nsp), line(line) {}
};

std::optional<SupportFileAndLine> GetDefaultFileAndLine();

bool DefaultFileAndLineSet() {
return (GetFile(m_last_support_file_sp).get() != nullptr);
return (GetFile(m_last_support_file_nsp).get() != nullptr);
}

void FindLinesMatchingRegex(lldb::SupportFileSP support_file_sp,
void FindLinesMatchingRegex(SupportFileNSP support_file_nsp,
RegularExpression &regex, uint32_t start_line,
uint32_t end_line,
std::vector<uint32_t> &match_lines);

FileSP GetFile(lldb::SupportFileSP support_file_sp);
FileSP GetFile(SupportFileNSP support_file_nsp);

protected:
lldb::SupportFileSP m_last_support_file_sp;
SupportFileNSP m_last_support_file_nsp;
uint32_t m_last_line;
uint32_t m_last_count;
bool m_default_set;
Expand Down
12 changes: 6 additions & 6 deletions lldb/include/lldb/Symbol/CompileUnit.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class CompileUnit : public std::enable_shared_from_this<CompileUnit>,
/// \param[in] user_data
/// User data where the SymbolFile parser can store data.
///
/// \param[in] support_file_sp
/// \param[in] support_file_nsp
/// The file specification for the source file of this compile
/// unit.
///
Expand All @@ -118,7 +118,7 @@ class CompileUnit : public std::enable_shared_from_this<CompileUnit>,
/// An rvalue list of already parsed support files.
/// \see lldb::LanguageType
CompileUnit(const lldb::ModuleSP &module_sp, void *user_data,
lldb::SupportFileSP support_file_sp, lldb::user_id_t uid,
SupportFileNSP support_file_nsp, lldb::user_id_t uid,
lldb::LanguageType language, lldb_private::LazyBool is_optimized,
SupportFileList &&support_files = {});

Expand Down Expand Up @@ -230,12 +230,12 @@ class CompileUnit : public std::enable_shared_from_this<CompileUnit>,

/// Return the primary source spec associated with this compile unit.
const FileSpec &GetPrimaryFile() const {
return m_primary_support_file_sp->GetSpecOnly();
return m_primary_support_file_nsp->GetSpecOnly();
}

/// Return the primary source file associated with this compile unit.
lldb::SupportFileSP GetPrimarySupportFile() const {
return m_primary_support_file_sp;
SupportFileNSP GetPrimarySupportFile() const {
return m_primary_support_file_nsp;
}

/// Get the line table for the compile unit.
Expand Down Expand Up @@ -430,7 +430,7 @@ class CompileUnit : public std::enable_shared_from_this<CompileUnit>,
/// compile unit.
std::vector<SourceModule> m_imported_modules;
/// The primary file associated with this compile unit.
lldb::SupportFileSP m_primary_support_file_sp;
SupportFileNSP m_primary_support_file_nsp;
/// Files associated with this compile unit's line table and declarations.
SupportFileList m_support_files;
/// Line table that will get parsed on demand.
Expand Down
4 changes: 2 additions & 2 deletions lldb/include/lldb/Symbol/Function.h
Original file line number Diff line number Diff line change
Expand Up @@ -473,12 +473,12 @@ class Function : public UserID, public SymbolContextScope {
///
/// \param[out] line_no
/// The line number.
void GetStartLineSourceInfo(lldb::SupportFileSP &source_file_sp,
void GetStartLineSourceInfo(SupportFileNSP &source_file_sp,
uint32_t &line_no);

using SourceRange = Range<uint32_t, uint32_t>;
/// Find the file and line number range of the function.
llvm::Expected<std::pair<lldb::SupportFileSP, SourceRange>> GetSourceInfo();
llvm::Expected<std::pair<SupportFileNSP, SourceRange>> GetSourceInfo();

/// Get the outgoing call edges from this function, sorted by their return
/// PC addresses (in increasing order).
Expand Down
4 changes: 2 additions & 2 deletions lldb/include/lldb/Symbol/LineEntry.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,10 @@ struct LineEntry {
AddressRange range;

/// The source file, possibly mapped by the target.source-map setting.
lldb::SupportFileSP file_sp;
SupportFileNSP file_sp;

/// The original source file, from debug info.
lldb::SupportFileSP original_file_sp;
SupportFileNSP original_file_sp;

/// The source line number, or LLDB_INVALID_LINE_NUMBER if there is no line
/// number information.
Expand Down
2 changes: 1 addition & 1 deletion lldb/include/lldb/Utility/FileSpecList.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class SupportFileList {
bool AppendIfUnique(const FileSpec &file);
size_t GetSize() const { return m_files.size(); }
const FileSpec &GetFileSpecAtIndex(size_t idx) const;
lldb::SupportFileSP GetSupportFileAtIndex(size_t idx) const;
SupportFileNSP GetSupportFileAtIndex(size_t idx) const;
size_t FindFileIndex(size_t idx, const FileSpec &file, bool full) const;
/// Find a compatible file index.
///
Expand Down
80 changes: 80 additions & 0 deletions lldb/include/lldb/Utility/NonNullSharedPtr.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLDB_UTILITY_NONNULLSHAREDPTR_H
#define LLDB_UTILITY_NONNULLSHAREDPTR_H

#include <memory>
#include <utility>

namespace lldb_private {

/// A non-nullable shared pointer that always holds a valid object.
///
/// NonNullSharedPtr is a smart pointer wrapper around std::shared_ptr that
/// guarantees the pointer is never null.
///
/// This class is used for enforcing invariants at the type level and
/// eliminating entire classes of null pointer bugs.
///
/// @tparam T The type of object to manage. Must be default-constructible.
template <typename T> class NonNullSharedPtr : private std::shared_ptr<T> {
using Base = std::shared_ptr<T>;

public:
NonNullSharedPtr(const std::shared_ptr<T> &t)
: Base(t ? t : std::make_shared<T>()) {
assert(t && "NonNullSharedPtr initialized from NULL shared_ptr");
}

NonNullSharedPtr(std::shared_ptr<T> &&t)
: Base(t ? std::move(t) : std::make_shared<T>()) {
// Can't assert on t as it's been moved-from.
}

NonNullSharedPtr(const NonNullSharedPtr &other) : Base(other) {}

NonNullSharedPtr(NonNullSharedPtr &&other) : Base(std::move(other)) {}

NonNullSharedPtr &operator=(const NonNullSharedPtr &other) {
Base::operator=(other);
return *this;
}

NonNullSharedPtr &operator=(NonNullSharedPtr &&other) {
Base::operator=(std::move(other));
return *this;
}

using Base::operator*;
using Base::operator->;
using Base::get;
using Base::unique;
using Base::use_count;
using Base::operator bool;

void swap(NonNullSharedPtr &other) { Base::swap(other); }

/// Explicitly deleted operations that could introduce nullptr.
/// @{
void reset() = delete;
void reset(T *ptr) = delete;
/// @}
};

} // namespace lldb_private

/// Specialized swap function for NonNullSharedPtr to enable argument-dependent
/// lookup (ADL) and efficient swapping.
template <typename T>
void swap(lldb_private::NonNullSharedPtr<T> &lhs,
lldb_private::NonNullSharedPtr<T> &rhs) {
lhs.swap(rhs);
}

#endif
3 changes: 3 additions & 0 deletions lldb/include/lldb/Utility/SupportFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include "lldb/Utility/Checksum.h"
#include "lldb/Utility/FileSpec.h"
#include "lldb/Utility/NonNullSharedPtr.h"

namespace lldb_private {

Expand Down Expand Up @@ -76,6 +77,8 @@ class SupportFile {
const Checksum m_checksum;
};

typedef NonNullSharedPtr<lldb_private::SupportFile> SupportFileNSP;

} // namespace lldb_private

#endif // LLDB_UTILITY_SUPPORTFILE_H
1 change: 0 additions & 1 deletion lldb/include/lldb/lldb-forward.h
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,6 @@ typedef std::shared_ptr<lldb_private::TypeSummaryImpl> TypeSummaryImplSP;
typedef std::shared_ptr<lldb_private::TypeSummaryOptions> TypeSummaryOptionsSP;
typedef std::shared_ptr<lldb_private::ScriptedSyntheticChildren>
ScriptedSyntheticChildrenSP;
typedef std::shared_ptr<lldb_private::SupportFile> SupportFileSP;
typedef std::shared_ptr<lldb_private::UnixSignals> UnixSignalsSP;
typedef std::weak_ptr<lldb_private::UnixSignals> UnixSignalsWP;
typedef std::shared_ptr<lldb_private::UnwindAssembly> UnwindAssemblySP;
Expand Down
2 changes: 1 addition & 1 deletion lldb/source/Breakpoint/BreakpointResolverFileLine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ void BreakpointResolverFileLine::FilterContexts(SymbolContextList &sc_list) {
if (!sc.block)
continue;

SupportFileSP file_sp;
SupportFileNSP file_sp = std::make_shared<SupportFile>();
uint32_t line;
const Block *inline_block = sc.block->GetContainingInlinedBlock();
if (inline_block) {
Expand Down
2 changes: 1 addition & 1 deletion lldb/source/Commands/CommandObjectBreakpoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,7 @@ class CommandObjectBreakpointSet : public CommandObjectParsed {
// frame's file.
if (auto maybe_file_and_line =
target.GetSourceManager().GetDefaultFileAndLine()) {
file = maybe_file_and_line->support_file_sp->GetSpecOnly();
file = maybe_file_and_line->support_file_nsp->GetSpecOnly();
return true;
}

Expand Down
4 changes: 2 additions & 2 deletions lldb/source/Commands/CommandObjectSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,7 @@ class CommandObjectSourceList : public CommandObjectParsed {
if (sc.function) {
Target &target = GetTarget();

SupportFileSP start_file = std::make_shared<SupportFile>();
SupportFileNSP start_file = std::make_shared<SupportFile>();
uint32_t start_line;
uint32_t end_line;
FileSpec end_file;
Expand Down Expand Up @@ -1194,7 +1194,7 @@ class CommandObjectSourceList : public CommandObjectParsed {
// file(s) will be found and assigned to
// sc.comp_unit->GetPrimarySupportFile, which is NOT what we want to
// print. Instead, we want to print the one from the line entry.
lldb::SupportFileSP found_file_sp = sc.line_entry.file_sp;
SupportFileNSP found_file_sp = sc.line_entry.file_sp;

target.GetSourceManager().DisplaySourceLinesWithLineNumbers(
found_file_sp, m_options.start_line, column, 0,
Expand Down
5 changes: 3 additions & 2 deletions lldb/source/Core/Disassembler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ Disassembler::GetFunctionDeclLineEntry(const SymbolContext &sc) {
return {};

LineEntry prologue_end_line = sc.line_entry;
SupportFileSP func_decl_file_sp;
SupportFileNSP func_decl_file_sp = std::make_shared<SupportFile>();
uint32_t func_decl_line;
sc.function->GetStartLineSourceInfo(func_decl_file_sp, func_decl_line);

Expand Down Expand Up @@ -411,7 +411,8 @@ void Disassembler::PrintInstructions(Debugger &debugger, const ArchSpec &arch,
LineEntry prologue_end_line = sc.line_entry;
if (!ElideMixedSourceAndDisassemblyLine(exe_ctx, sc,
prologue_end_line)) {
SupportFileSP func_decl_file_sp;
SupportFileNSP func_decl_file_sp =
std::make_shared<SupportFile>();
uint32_t func_decl_line;
sc.function->GetStartLineSourceInfo(func_decl_file_sp,
func_decl_line);
Expand Down
Loading