Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
30 changes: 15 additions & 15 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(SupportFileSP support_file_sp, lldb::TargetSP target_sp);
File(SupportFileSP support_file_sp, lldb::DebuggerSP debugger_sp);

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

bool LineIsValid(uint32_t line);

lldb::SupportFileSP GetSupportFile() const {
SupportFileSP GetSupportFile() const {
assert(m_support_file_sp && "SupportFileSP must always be valid");
return m_support_file_sp;
}
Expand All @@ -80,13 +81,13 @@ class SourceManager {

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

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;
SupportFileSP m_support_file_sp;

/// 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(SupportFileSP support_file_sp,
lldb::TargetSP target_sp);
void CommonInitializerImpl(lldb::SupportFileSP support_file_sp,
void CommonInitializerImpl(SupportFileSP support_file_sp,
lldb::TargetSP target_sp);
};

Expand Down Expand Up @@ -162,7 +163,7 @@ class SourceManager {
}

size_t DisplaySourceLinesWithLineNumbers(
lldb::SupportFileSP support_file_sp, uint32_t line, uint32_t column,
SupportFileSP support_file_sp, 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,13 +177,12 @@ 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(SupportFileSP support_file_sp, uint32_t line);

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

Expand All @@ -192,15 +192,15 @@ class SourceManager {
return (GetFile(m_last_support_file_sp).get() != nullptr);
}

void FindLinesMatchingRegex(lldb::SupportFileSP support_file_sp,
void FindLinesMatchingRegex(SupportFileSP support_file_sp,
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(SupportFileSP support_file_sp);

protected:
lldb::SupportFileSP m_last_support_file_sp;
SupportFileSP m_last_support_file_sp;
uint32_t m_last_line;
uint32_t m_last_count;
bool m_default_set;
Expand Down
6 changes: 3 additions & 3 deletions lldb/include/lldb/Symbol/CompileUnit.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
SupportFileSP support_file_sp, lldb::user_id_t uid,
lldb::LanguageType language, lldb_private::LazyBool is_optimized,
SupportFileList &&support_files = {});

Expand Down Expand Up @@ -234,7 +234,7 @@ class CompileUnit : public std::enable_shared_from_this<CompileUnit>,
}

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

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;
SupportFileSP m_primary_support_file_sp;
/// 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
5 changes: 2 additions & 3 deletions lldb/include/lldb/Symbol/Function.h
Original file line number Diff line number Diff line change
Expand Up @@ -469,12 +469,11 @@ class Function : public UserID, public SymbolContextScope {
///
/// \param[out] line_no
/// The line number.
void GetStartLineSourceInfo(lldb::SupportFileSP &source_file_sp,
uint32_t &line_no);
void GetStartLineSourceInfo(SupportFileSP &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<SupportFileSP, 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;
SupportFileSP file_sp;

/// The original source file, from debug info.
lldb::SupportFileSP original_file_sp;
SupportFileSP 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;
SupportFileSP GetSupportFileAtIndex(size_t idx) const;
size_t FindFileIndex(size_t idx, const FileSpec &file, bool full) const;
/// Find a compatible file index.
///
Expand Down
96 changes: 96 additions & 0 deletions lldb/include/lldb/Utility/NonNullSharedPtr.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
//===----------------------------------------------------------------------===//
//
// 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. If default-constructed, it creates
/// a default-constructed instance of T.
///
/// 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() : Base(std::make_shared<T>()) {}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this assert that make_shared succeeded in creating a non-null shared_ptr? It's rare but we have exceptions disabled, so this will violate the invariant under your nose and you'll crash somewhere later on anyway.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't mind adding it, but at the same time, if malloc failed, we have bigger issues, and nothing in LLDB is resilient against that... so it would just be theater.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I merged the PR but happy to revisit this post-commit if you disagree with my conclusion.

Comment thread
JDevlieghere marked this conversation as resolved.
Outdated

NonNullSharedPtr(const std::shared_ptr<T> &t)
: Base(t ? t : std::make_shared<T>()) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do you want to support calling this class with nullptrs? Based on my "will this compile" question below, I suspect we really don't want t to ever be null here.

assert(t && "NonNullSharedPtr initialized from NULL shared_ptr");
}

NonNullSharedPtr(std::shared_ptr<T> &&t)
: Base(t ? std::move(t) : std::make_shared<T>()) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this compile for types that cannot be default constructed?

Also, I think here you can just make a helper function that asserts and returns.

// Can't assert on t as it's been moved-from.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can't assert on t, but you can assert that you're non-null, right?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but that could only happen if make_shared failed, and then we're in the scenario described in the previous comment.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given the LineTableTest path, which would silently create an empty object, should we instead unconditionally move t in Base(), and in the constructor's body we can assert if it's null, and replace it with a default constructed value?

  NonNullSharedPtr(std::shared_ptr<T> &&t)
      : Base(std::move(t)) {
  assert(*this); // invoke operator bool here
  *this = std::make_shared<T>();
}

Not sure if this snippet works

}

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

NonNullSharedPtr(NonNullSharedPtr &&other) noexcept
Comment thread
JDevlieghere marked this conversation as resolved.
Outdated
: Base(std::move(other)) {}

NonNullSharedPtr &operator=(const NonNullSharedPtr &other) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this different from = default? Likewise for the below

Base::operator=(other);
return *this;
}

NonNullSharedPtr &operator=(NonNullSharedPtr &&other) noexcept {
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) noexcept { Base::swap(other); }

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

} // namespace lldb_private

template <typename T>
bool operator==(const lldb_private::NonNullSharedPtr<T> &lhs,
Comment thread
JDevlieghere marked this conversation as resolved.
Outdated
const lldb_private::NonNullSharedPtr<T> &rhs) {
return lhs.get() == rhs.get();
}

template <typename T>
bool operator!=(const lldb_private::NonNullSharedPtr<T> &lhs,
const lldb_private::NonNullSharedPtr<T> &rhs) {
return !(lhs == rhs);
}

/// 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) noexcept {
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> SupportFileSP;
Comment thread
JDevlieghere marked this conversation as resolved.
Outdated

} // 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 @@ -493,7 +493,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/Commands/CommandObjectSource.cpp
Original file line number Diff line number Diff line change
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;
SupportFileSP found_file_sp = sc.line_entry.file_sp;

target.GetSourceManager().DisplaySourceLinesWithLineNumbers(
found_file_sp, m_options.start_line, column, 0,
Expand Down
7 changes: 4 additions & 3 deletions lldb/source/Core/SourceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "lldb/Utility/Log.h"
#include "lldb/Utility/RegularExpression.h"
#include "lldb/Utility/Stream.h"
#include "lldb/Utility/SupportFile.h"
#include "lldb/lldb-enumerations.h"

#include "llvm/ADT/Twine.h"
Expand Down Expand Up @@ -325,7 +326,7 @@ size_t SourceManager::DisplaySourceLinesWithLineNumbersUsingLastFile(
}

size_t SourceManager::DisplaySourceLinesWithLineNumbers(
lldb::SupportFileSP support_file_sp, uint32_t line, uint32_t column,
SupportFileSP support_file_sp, 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) {
Expand Down Expand Up @@ -389,7 +390,7 @@ size_t SourceManager::DisplayMoreWithLineNumbers(
return 0;
}

bool SourceManager::SetDefaultFileAndLine(lldb::SupportFileSP support_file_sp,
bool SourceManager::SetDefaultFileAndLine(SupportFileSP support_file_sp,
uint32_t line) {
assert(support_file_sp && "SupportFile must be valid");

Expand Down Expand Up @@ -575,7 +576,7 @@ void SourceManager::File::CommonInitializerImpl(SupportFileSP support_file_sp,
}
}

void SourceManager::File::SetSupportFile(lldb::SupportFileSP support_file_sp) {
void SourceManager::File::SetSupportFile(SupportFileSP support_file_sp) {
FileSpec file_spec = support_file_sp->GetSpecOnly();
resolve_tilde(file_spec);
m_support_file_sp =
Expand Down
2 changes: 1 addition & 1 deletion lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,7 @@ lldb::CompUnitSP SymbolFileDWARF::ParseCompileUnit(DWARFCompileUnit &dwarf_cu) {
} else {
ModuleSP module_sp(m_objfile_sp->GetModule());
if (module_sp) {
auto initialize_cu = [&](lldb::SupportFileSP support_file_sp,
auto initialize_cu = [&](SupportFileSP support_file_sp,
LanguageType cu_language,
SupportFileList &&support_files = {}) {
BuildCuTranslationTable();
Expand Down
2 changes: 1 addition & 1 deletion lldb/source/Symbol/CompileUnit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ CompileUnit::CompileUnit(const lldb::ModuleSP &module_sp, void *user_data,
language, is_optimized) {}

CompileUnit::CompileUnit(const lldb::ModuleSP &module_sp, void *user_data,
lldb::SupportFileSP support_file_sp,
SupportFileSP support_file_sp,
const lldb::user_id_t cu_sym_id,
lldb::LanguageType language,
lldb_private::LazyBool is_optimized,
Expand Down
4 changes: 2 additions & 2 deletions lldb/source/Target/ThreadPlanStepRange.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -431,10 +431,10 @@ bool ThreadPlanStepRange::SetNextBranchBreakpoint() {
top_most_line_entry.original_file_sp =
std::make_shared<SupportFile>(call_site_file_spec);
top_most_line_entry.range = range;
top_most_line_entry.file_sp.reset();
top_most_line_entry.file_sp = std::make_shared<SupportFile>();
top_most_line_entry.ApplyFileMappings(
GetThread().CalculateTarget());
if (!top_most_line_entry.file_sp)
if (!top_most_line_entry.file_sp->GetSpecOnly())
Comment thread
augusto2112 marked this conversation as resolved.
top_most_line_entry.file_sp =
top_most_line_entry.original_file_sp;
}
Expand Down
10 changes: 4 additions & 6 deletions lldb/source/Utility/FileSpecList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,9 @@ bool FileSpecList::AppendIfUnique(const FileSpec &file_spec) {
// FIXME: Replace this with a DenseSet at the call site. It is inefficient.
bool SupportFileList::AppendIfUnique(const FileSpec &file_spec) {
collection::iterator end = m_files.end();
if (find_if(m_files.begin(), end,
[&](const std::shared_ptr<SupportFile> &support_file) {
return support_file->GetSpecOnly() == file_spec;
}) == end) {
if (find_if(m_files.begin(), end, [&](const SupportFileSP &support_file) {
return support_file->GetSpecOnly() == file_spec;
}) == end) {
Append(file_spec);
return true;
}
Expand Down Expand Up @@ -214,8 +213,7 @@ const FileSpec &SupportFileList::GetFileSpecAtIndex(size_t idx) const {
return g_empty_file_spec;
}

std::shared_ptr<SupportFile>
SupportFileList::GetSupportFileAtIndex(size_t idx) const {
SupportFileSP SupportFileList::GetSupportFileAtIndex(size_t idx) const {
if (idx < m_files.size())
return m_files[idx];
return {};
Expand Down
Loading