llvm-project/lldb/source/Symbol/Declaration.cpp
Pavel Labath 532290e69f [lldb] s/FileSpec::Equal/FileSpec::Match
Summary:
The FileSpec class is often used as a sort of a pattern -- one specifies
a bare file name to search, and we check if in matches the full file
name of an existing module (for example).

These comparisons used FileSpec::Equal, which had some support for it
(via the full=false argument), but it was not a good fit for this job.

For one, it did a symmetric comparison, which makes sense for a function
called "equal", but not for typical searches (when searching for
"/foo/bar.so", we don't want to find a module whose name is just
"bar.so"). This resulted in patterns like:
    if (FileSpec::Equal(pattern, file, pattern.GetDirectory()))
which would request a "full" match only if the pattern really contained
a directory. This worked, but the intended behavior was very unobvious.

On top of that, a lot of the code wanted to handle the case of an
"empty" pattern, and treat it as matching everything. This resulted in
conditions like:
    if (pattern && !FileSpec::Equal(pattern, file, pattern.GetDirectory())
which are nearly impossible to decipher.

This patch introduces a FileSpec::Match function, which does exactly
what most of FileSpec::Equal callers want, an asymmetric match between a
"pattern" FileSpec and a an actual FileSpec. Empty paterns match
everything, filename-only patterns match only the filename component.

I've tried to update all callers of FileSpec::Equal to use a simpler
interface. Those that hardcoded full=true have been changed to use
operator==. Those passing full=pattern.GetDirectory() have been changed
to use FileSpec::Match.

There was also a handful of places which hardcoded full=false. I've
changed these to use FileSpec::Match too. This is a slight change in
semantics, but it does not look like that was ever intended, and it was
more likely a result of a misunderstanding of the "proper" way to use
FileSpec::Equal.

[In an ideal world a "FileSpec" and a "FileSpec pattern" would be two
different types, but given how widespread FileSpec is, it is unlikely
we'll get there in one go. This at least provides a good starting point
by centralizing all matching behavior.]

Reviewers: teemperor, JDevlieghere, jdoerfert

Subscribers: emaste, lldb-commits

Tags: #lldb

Differential Revision: https://reviews.llvm.org/D70851
2019-12-04 10:42:32 +01:00

99 lines
2.6 KiB
C++

//===-- Declaration.cpp -----------------------------------------*- C++ -*-===//
//
// 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 "lldb/Symbol/Declaration.h"
#include "lldb/Utility/Stream.h"
using namespace lldb_private;
void Declaration::Dump(Stream *s, bool show_fullpaths) const {
if (m_file) {
*s << ", decl = ";
if (show_fullpaths)
*s << m_file;
else
*s << m_file.GetFilename();
if (m_line > 0)
s->Printf(":%u", m_line);
#ifdef LLDB_ENABLE_DECLARATION_COLUMNS
if (m_column > 0)
s->Printf(":%u", m_column);
#endif
} else {
if (m_line > 0) {
s->Printf(", line = %u", m_line);
#ifdef LLDB_ENABLE_DECLARATION_COLUMNS
if (m_column > 0)
s->Printf(":%u", m_column);
#endif
}
#ifdef LLDB_ENABLE_DECLARATION_COLUMNS
else if (m_column > 0)
s->Printf(", column = %u", m_column);
#endif
}
}
bool Declaration::DumpStopContext(Stream *s, bool show_fullpaths) const {
if (m_file) {
if (show_fullpaths)
*s << m_file;
else
m_file.GetFilename().Dump(s);
if (m_line > 0)
s->Printf(":%u", m_line);
#ifdef LLDB_ENABLE_DECLARATION_COLUMNS
if (m_column > 0)
s->Printf(":%u", m_column);
#endif
return true;
} else if (m_line > 0) {
s->Printf(" line %u", m_line);
#ifdef LLDB_ENABLE_DECLARATION_COLUMNS
if (m_column > 0)
s->Printf(":%u", m_column);
#endif
return true;
}
return false;
}
size_t Declaration::MemorySize() const { return sizeof(Declaration); }
int Declaration::Compare(const Declaration &a, const Declaration &b) {
int result = FileSpec::Compare(a.m_file, b.m_file, true);
if (result)
return result;
if (a.m_line < b.m_line)
return -1;
else if (a.m_line > b.m_line)
return 1;
#ifdef LLDB_ENABLE_DECLARATION_COLUMNS
if (a.m_column < b.m_column)
return -1;
else if (a.m_column > b.m_column)
return 1;
#endif
return 0;
}
bool Declaration::FileAndLineEqual(const Declaration &declaration) const {
int file_compare = FileSpec::Compare(this->m_file, declaration.m_file, true);
return file_compare == 0 && this->m_line == declaration.m_line;
}
bool lldb_private::operator==(const Declaration &lhs, const Declaration &rhs) {
#ifdef LLDB_ENABLE_DECLARATION_COLUMNS
if (lhs.GetColumn() != rhs.GetColumn())
return false;
#else
return lhs.GetLine() == rhs.GetLine() && lhs.GetFile() == rhs.GetFile();
#endif
}