Skip to content

Commit

Permalink
FileManager: Improve the FileEntryRef API and customize its OptionalS…
Browse files Browse the repository at this point in the history
…torage

Make a few changes to the `FileEntryRef` API in preparation for
propagating it enough to remove `FileEntry::getName()`.

- Allow `FileEntryRef` to degrade implicitly to `const FileEntry*`. This
  allows functions currently returning `const FileEntry *` to be updated
  to return `FileEntryRef` without requiring all callers to be updated
  in the same patch. This helps avoid both (a) massive patches where
  many fields and locals are updated simultaneously and (b) noisy
  incremental patches where the first patch adds `getFileEntry()` at
  call sites and the second patch removes it. (Once `FileEntryRef` is
  everywhere, we should remove this API.)
- Change `operator==` to compare the underlying `FileEntry*`, ignoring
  any difference in the spelling of the filename. There were 0 users of
  the existing function because it's not useful.  In case comparing the
  exact named reference becomes important, add/test `isSameRef`.
- Add `==` comparisons between `FileEntryRef` and `const FileEntry *`
  (compares the `FileEntry*`).
- Customize `OptionalStorage<FileEntryRef>` to be pointer-sized. Add
  a private constructor that initializes with `nullptr` and specialize
  `OptionalStorage` to use it. This unblocks updating fields in
  size-sensitive data structures that currently use `const FileEntry *`.
- Add `OptionalFileEntryRefDegradesToFileEntryPtr`, a wrapper around
  `Optional<FileEntryRef>` that degrades to `const FileEntry*`. This
  facilitates future incremental patches, like the same operator on
  `FileEntryRef`. (Once `FileEntryRef` is everywhere, we should remove
  this class.)
- Remove the unncessary `const` from the by-value return of
  `FileEntryRef::getName`.
- Delete the unused function `FileEntry::isOpenForTests`.

Note that there are still `FileEntry` APIs that aren't wrapped and I
plan to deal with these separately / incrementally, as they are needed.

Differential Revision: https://reviews.llvm.org/D89834
  • Loading branch information
dexonsmith committed Oct 30, 2020
1 parent 2177e45 commit 84e8257
Show file tree
Hide file tree
Showing 4 changed files with 368 additions and 15 deletions.
226 changes: 211 additions & 15 deletions clang/include/clang/Basic/FileEntry.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,32 @@ class File;

namespace clang {

class FileEntryRef;

} // namespace clang

namespace llvm {
namespace optional_detail {

/// Forward declare a template specialization for OptionalStorage.
template <>
class OptionalStorage<clang::FileEntryRef, /*is_trivially_copyable*/ true>;

} // namespace optional_detail
} // namespace llvm

namespace clang {

class DirectoryEntry;
class FileEntry;

/// A reference to a \c FileEntry that includes the name of the file as it was
/// accessed by the FileManager's client.
class FileEntryRef {
public:
const StringRef getName() const { return Entry->first(); }
StringRef getName() const { return ME->first(); }
const FileEntry &getFileEntry() const {
return *Entry->second->V.get<FileEntry *>();
return *ME->second->V.get<FileEntry *>();
}

inline bool isValid() const;
Expand All @@ -49,12 +65,26 @@ class FileEntryRef {
inline const llvm::sys::fs::UniqueID &getUniqueID() const;
inline time_t getModificationTime() const;

/// Check if the underlying FileEntry is the same, intentially ignoring
/// whether the file was referenced with the same spelling of the filename.
friend bool operator==(const FileEntryRef &LHS, const FileEntryRef &RHS) {
return LHS.Entry == RHS.Entry;
return &LHS.getFileEntry() == &RHS.getFileEntry();
}
friend bool operator==(const FileEntry *LHS, const FileEntryRef &RHS) {
return LHS == &RHS.getFileEntry();
}
friend bool operator==(const FileEntryRef &LHS, const FileEntry *RHS) {
return &LHS.getFileEntry() == RHS;
}
friend bool operator!=(const FileEntryRef &LHS, const FileEntryRef &RHS) {
return !(LHS == RHS);
}
friend bool operator!=(const FileEntry *LHS, const FileEntryRef &RHS) {
return !(LHS == RHS);
}
friend bool operator!=(const FileEntryRef &LHS, const FileEntry *RHS) {
return !(LHS == RHS);
}

struct MapValue;

Expand All @@ -78,20 +108,190 @@ class FileEntryRef {
MapValue(MapEntry &ME) : V(&ME) {}
};

private:
friend class FileManager;
/// Check if RHS referenced the file in exactly the same way.
bool isSameRef(const FileEntryRef &RHS) const { return ME == RHS.ME; }

/// Allow FileEntryRef to degrade into 'const FileEntry*' to facilitate
/// incremental adoption.
///
/// The goal is to avoid code churn due to dances like the following:
/// \code
/// // Old code.
/// lvalue = rvalue;
///
/// // Temporary code from an incremental patch.
/// lvalue = &rvalue.getFileEntry();
///
/// // Final code.
/// lvalue = rvalue;
/// \endcode
///
/// FIXME: Once FileEntryRef is "everywhere" and FileEntry::LastRef and
/// FileEntry::getName have been deleted, delete this implicit conversion.
operator const FileEntry *() const { return &getFileEntry(); }

FileEntryRef() = delete;
explicit FileEntryRef(const MapEntry &Entry)
: Entry(&Entry) {
assert(Entry.second && "Expected payload");
assert(Entry.second->V && "Expected non-null");
assert(Entry.second->V.is<FileEntry *>() && "Expected FileEntry");
explicit FileEntryRef(const MapEntry &ME) : ME(&ME) {
assert(ME.second && "Expected payload");
assert(ME.second->V && "Expected non-null");
assert(ME.second->V.is<FileEntry *>() && "Expected FileEntry");
}

/// Expose the underlying MapEntry to simplify packing in a PointerIntPair or
/// PointerUnion and allow construction in Optional.
const clang::FileEntryRef::MapEntry &getMapEntry() const { return *ME; }

private:
friend class llvm::optional_detail::OptionalStorage<
FileEntryRef, /*is_trivially_copyable*/ true>;
struct optional_none_tag {};

// Private constructor for use by OptionalStorage.
FileEntryRef(optional_none_tag) : ME(nullptr) {}
bool hasOptionalValue() const { return ME; }

const MapEntry *ME;
};

static_assert(sizeof(FileEntryRef) == sizeof(const FileEntry *),
"FileEntryRef must avoid size overhead");

static_assert(std::is_trivially_copyable<FileEntryRef>::value,
"FileEntryRef must be trivially copyable");

} // end namespace clang

namespace llvm {
namespace optional_detail {

/// Customize OptionalStorage<FileEntryRef> to use FileEntryRef and its
/// optional_none_tag to keep it the size of a single pointer.
template <> class OptionalStorage<clang::FileEntryRef> {
clang::FileEntryRef MaybeRef = clang::FileEntryRef::optional_none_tag{};

public:
~OptionalStorage() = default;
constexpr OptionalStorage() noexcept = default;
constexpr OptionalStorage(OptionalStorage const &Other) = default;
constexpr OptionalStorage(OptionalStorage &&Other) = default;
OptionalStorage &operator=(OptionalStorage const &Other) = default;
OptionalStorage &operator=(OptionalStorage &&Other) = default;

template <class... ArgTypes>
constexpr explicit OptionalStorage(in_place_t, ArgTypes &&...Args)
: MaybeRef(std::forward<ArgTypes>(Args)...) {}

void reset() noexcept { MaybeRef = clang::FileEntryRef::optional_none_tag(); }

constexpr bool hasValue() const noexcept {
return MaybeRef.hasOptionalValue();
}

clang::FileEntryRef &getValue() LLVM_LVALUE_FUNCTION noexcept {
assert(hasValue());
return MaybeRef;
}
constexpr clang::FileEntryRef const &
getValue() const LLVM_LVALUE_FUNCTION noexcept {
assert(hasValue());
return MaybeRef;
}
#if LLVM_HAS_RVALUE_REFERENCE_THIS
clang::FileEntryRef &&getValue() &&noexcept {
assert(hasValue());
return std::move(MaybeRef);
}
#endif

template <class... Args> void emplace(Args &&...args) {
MaybeRef = clang::FileEntryRef(std::forward<Args>(args)...);
}

const MapEntry *Entry;
OptionalStorage &operator=(clang::FileEntryRef Ref) {
MaybeRef = Ref;
return *this;
}
};

static_assert(sizeof(Optional<clang::FileEntryRef>) ==
sizeof(clang::FileEntryRef),
"Optional<FileEntryRef> must avoid size overhead");

static_assert(std::is_trivially_copyable<Optional<clang::FileEntryRef>>::value,
"Optional<FileEntryRef> should be trivially copyable");

} // end namespace optional_detail
} // end namespace llvm

namespace clang {

/// Wrapper around Optional<FileEntryRef> that degrades to 'const FileEntry*',
/// facilitating incremental patches to propagate FileEntryRef.
///
/// This class can be used as return value or field where it's convenient for
/// an Optional<FileEntryRef> to degrade to a 'const FileEntry*'. The purpose
/// is to avoid code churn due to dances like the following:
/// \code
/// // Old code.
/// lvalue = rvalue;
///
/// // Temporary code from an incremental patch.
/// Optional<FileEntryRef> MaybeF = rvalue;
/// lvalue = MaybeF ? &MaybeF.getFileEntry() : nullptr;
///
/// // Final code.
/// lvalue = rvalue;
/// \endcode
///
/// FIXME: Once FileEntryRef is "everywhere" and FileEntry::LastRef and
/// FileEntry::getName have been deleted, delete this class and replace
/// instances with Optional<FileEntryRef>.
class OptionalFileEntryRefDegradesToFileEntryPtr
: public Optional<FileEntryRef> {
public:
constexpr OptionalFileEntryRefDegradesToFileEntryPtr() noexcept = default;
constexpr OptionalFileEntryRefDegradesToFileEntryPtr(
OptionalFileEntryRefDegradesToFileEntryPtr &&) = default;
constexpr OptionalFileEntryRefDegradesToFileEntryPtr(
const OptionalFileEntryRefDegradesToFileEntryPtr &) = default;
OptionalFileEntryRefDegradesToFileEntryPtr &
operator=(OptionalFileEntryRefDegradesToFileEntryPtr &&) = default;
OptionalFileEntryRefDegradesToFileEntryPtr &
operator=(const OptionalFileEntryRefDegradesToFileEntryPtr &) = default;

OptionalFileEntryRefDegradesToFileEntryPtr(llvm::NoneType) {}
OptionalFileEntryRefDegradesToFileEntryPtr(FileEntryRef Ref)
: Optional<FileEntryRef>(Ref) {}
OptionalFileEntryRefDegradesToFileEntryPtr(Optional<FileEntryRef> MaybeRef)
: Optional<FileEntryRef>(MaybeRef) {}

OptionalFileEntryRefDegradesToFileEntryPtr &operator=(llvm::NoneType) {
Optional<FileEntryRef>::operator=(None);
return *this;
}
OptionalFileEntryRefDegradesToFileEntryPtr &operator=(FileEntryRef Ref) {
Optional<FileEntryRef>::operator=(Ref);
return *this;
}
OptionalFileEntryRefDegradesToFileEntryPtr &
operator=(Optional<FileEntryRef> MaybeRef) {
Optional<FileEntryRef>::operator=(MaybeRef);
return *this;
}

/// Degrade to 'const FileEntry *' to allow FileEntry::LastRef and
/// FileEntry::getName have been deleted, delete this class and replace
/// instances with Optional<FileEntryRef>
operator const FileEntry *() const {
return hasValue() ? &getValue().getFileEntry() : nullptr;
}
};

static_assert(
std::is_trivially_copyable<
OptionalFileEntryRefDegradesToFileEntryPtr>::value,
"OptionalFileEntryRefDegradesToFileEntryPtr should be trivially copyable");

/// Cached information about one file (either on disk
/// or in the virtual file system).
///
Expand Down Expand Up @@ -147,10 +347,6 @@ class FileEntry {
bool isNamedPipe() const { return IsNamedPipe; }

void closeFile() const;

// Only for use in tests to see if deferred opens are happening, rather than
// relying on RealPathName being empty.
bool isOpenForTests() const { return File != nullptr; }
};

bool FileEntryRef::isValid() const { return getFileEntry().isValid(); }
Expand Down
1 change: 1 addition & 0 deletions clang/unittests/Basic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ set(LLVM_LINK_COMPONENTS
add_clang_unittest(BasicTests
CharInfoTest.cpp
DiagnosticTest.cpp
FileEntryTest.cpp
FileManagerTest.cpp
LineOffsetMappingTest.cpp
SourceManagerTest.cpp
Expand Down
104 changes: 104 additions & 0 deletions clang/unittests/Basic/FileEntryTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
//===- unittests/Basic/FileEntryTest.cpp - Test FileEntry/FileEntryRef ----===//
//
// 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
//
//===----------------------------------------------------------------------===//

#include "clang/Basic/FileEntry.h"
#include "llvm/ADT/StringMap.h"
#include "gtest/gtest.h"

using namespace llvm;
using namespace clang;

namespace {

using MapEntry = FileEntryRef::MapEntry;
using MapValue = FileEntryRef::MapValue;
using MapType = StringMap<llvm::ErrorOr<MapValue>>;

FileEntryRef addRef(MapType &M, StringRef Name, FileEntry &E) {
return FileEntryRef(*M.insert({Name, MapValue(E)}).first);
}

TEST(FileEntryTest, FileEntryRef) {
MapType Refs;
FileEntry E1, E2;
FileEntryRef R1 = addRef(Refs, "1", E1);
FileEntryRef R2 = addRef(Refs, "2", E2);
FileEntryRef R1Also = addRef(Refs, "1-also", E1);

EXPECT_EQ("1", R1.getName());
EXPECT_EQ("2", R2.getName());
EXPECT_EQ("1-also", R1Also.getName());

EXPECT_EQ(&E1, &R1.getFileEntry());
EXPECT_EQ(&E2, &R2.getFileEntry());
EXPECT_EQ(&E1, &R1Also.getFileEntry());

const FileEntry *CE1 = R1;
EXPECT_EQ(CE1, &E1);
}

TEST(FileEntryTest, OptionalFileEntryRefDegradesToFileEntryPtr) {
MapType Refs;
FileEntry E1, E2;
OptionalFileEntryRefDegradesToFileEntryPtr M0;
OptionalFileEntryRefDegradesToFileEntryPtr M1 = addRef(Refs, "1", E1);
OptionalFileEntryRefDegradesToFileEntryPtr M2 = addRef(Refs, "2", E2);
OptionalFileEntryRefDegradesToFileEntryPtr M0Also = None;
OptionalFileEntryRefDegradesToFileEntryPtr M1Also =
addRef(Refs, "1-also", E1);

EXPECT_EQ(M0, M0Also);
EXPECT_EQ(StringRef("1"), M1->getName());
EXPECT_EQ(StringRef("2"), M2->getName());
EXPECT_EQ(StringRef("1-also"), M1Also->getName());

EXPECT_EQ(&E1, &M1->getFileEntry());
EXPECT_EQ(&E2, &M2->getFileEntry());
EXPECT_EQ(&E1, &M1Also->getFileEntry());

const FileEntry *CE1 = M1;
EXPECT_EQ(CE1, &E1);
}

TEST(FileEntryTest, equals) {
MapType Refs;
FileEntry E1, E2;
FileEntryRef R1 = addRef(Refs, "1", E1);
FileEntryRef R2 = addRef(Refs, "2", E2);
FileEntryRef R1Also = addRef(Refs, "1-also", E1);

EXPECT_EQ(R1, &E1);
EXPECT_EQ(&E1, R1);
EXPECT_EQ(R1, R1Also);
EXPECT_NE(R1, &E2);
EXPECT_NE(&E2, R1);
EXPECT_NE(R1, R2);

OptionalFileEntryRefDegradesToFileEntryPtr M0;
OptionalFileEntryRefDegradesToFileEntryPtr M1 = R1;

EXPECT_EQ(M1, &E1);
EXPECT_EQ(&E1, M1);
EXPECT_NE(M1, &E2);
EXPECT_NE(&E2, M1);
}

TEST(FileEntryTest, isSameRef) {
MapType Refs;
FileEntry E1, E2;
FileEntryRef R1 = addRef(Refs, "1", E1);
FileEntryRef R2 = addRef(Refs, "2", E2);
FileEntryRef R1Also = addRef(Refs, "1-also", E1);

EXPECT_TRUE(R1.isSameRef(FileEntryRef(R1)));
EXPECT_TRUE(R1.isSameRef(FileEntryRef(R1.getMapEntry())));
EXPECT_FALSE(R1.isSameRef(R2));
EXPECT_FALSE(R1.isSameRef(R1Also));
}

} // end namespace
Loading

0 comments on commit 84e8257

Please sign in to comment.