cmake/Source/cmListFileCache.h

248 lines
7.2 KiB
C
Raw Normal View History

2016-10-30 18:24:19 +01:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
2021-09-14 00:13:48 +02:00
#pragma once
2017-07-20 19:35:53 +02:00
#include "cmConfigure.h" // IWYU pragma: keep
2016-10-30 18:24:19 +01:00
#include <iosfwd>
2020-02-01 23:06:01 +01:00
#include <memory>
2016-10-30 18:24:19 +01:00
#include <string>
2019-11-11 23:01:05 +01:00
#include <utility>
2016-10-30 18:24:19 +01:00
#include <vector>
2021-09-14 00:13:48 +02:00
#include <cm/optional>
2022-08-04 22:12:04 +02:00
#include "cmConstStack.h"
2023-07-02 19:51:09 +02:00
#include "cmList.h"
2021-09-14 00:13:48 +02:00
#include "cmSystemTools.h"
2015-04-27 22:25:09 +02:00
/** \class cmListFileCache
* \brief A class to cache list file contents.
*
* cmListFileCache is a class used to cache the contents of parsed
* cmake list files.
*/
2016-10-30 18:24:19 +01:00
class cmMessenger;
2013-03-16 19:13:01 +02:00
struct cmListFileArgument
{
2013-11-03 12:27:13 +02:00
enum Delimiter
2016-07-09 11:21:54 +02:00
{
2013-11-03 12:27:13 +02:00
Unquoted,
2014-08-03 19:52:23 +02:00
Quoted,
Bracket
2016-07-09 11:21:54 +02:00
};
2019-11-11 23:01:05 +01:00
cmListFileArgument() = default;
cmListFileArgument(std::string v, Delimiter d, long line)
: Value(std::move(v))
2016-07-09 11:21:54 +02:00
, Delim(d)
, Line(line)
{
}
bool operator==(const cmListFileArgument& r) const
{
2013-11-03 12:27:13 +02:00
return (this->Value == r.Value) && (this->Delim == r.Delim);
2016-07-09 11:21:54 +02:00
}
bool operator!=(const cmListFileArgument& r) const { return !(*this == r); }
std::string Value;
2019-11-11 23:01:05 +01:00
Delimiter Delim = Unquoted;
long Line = 0;
};
2021-09-14 00:13:48 +02:00
class cmListFileFunction
{
2021-09-14 00:13:48 +02:00
public:
2022-08-04 22:12:04 +02:00
cmListFileFunction(std::string name, long line, long lineEnd,
2021-09-14 00:13:48 +02:00
std::vector<cmListFileArgument> args)
2022-08-04 22:12:04 +02:00
: Impl{ std::make_shared<Implementation>(std::move(name), line, lineEnd,
2021-09-14 00:13:48 +02:00
std::move(args)) }
{
}
std::string const& OriginalName() const noexcept
{
2022-03-29 21:10:50 +02:00
return this->Impl->OriginalName;
2021-09-14 00:13:48 +02:00
}
std::string const& LowerCaseName() const noexcept
{
2022-03-29 21:10:50 +02:00
return this->Impl->LowerCaseName;
2021-09-14 00:13:48 +02:00
}
long Line() const noexcept { return this->Impl->Line; }
2022-08-04 22:12:04 +02:00
long LineEnd() const noexcept { return this->Impl->LineEnd; }
2021-09-14 00:13:48 +02:00
std::vector<cmListFileArgument> const& Arguments() const noexcept
{
return this->Impl->Arguments;
}
private:
2022-03-29 21:10:50 +02:00
struct Implementation
2021-09-14 00:13:48 +02:00
{
2022-08-04 22:12:04 +02:00
Implementation(std::string name, long line, long lineEnd,
2021-09-14 00:13:48 +02:00
std::vector<cmListFileArgument> args)
2022-03-29 21:10:50 +02:00
: OriginalName{ std::move(name) }
, LowerCaseName{ cmSystemTools::LowerCase(this->OriginalName) }
, Line{ line }
2022-08-04 22:12:04 +02:00
, LineEnd{ lineEnd }
2021-09-14 00:13:48 +02:00
, Arguments{ std::move(args) }
{
}
2022-03-29 21:10:50 +02:00
std::string OriginalName;
std::string LowerCaseName;
long Line = 0;
2022-08-04 22:12:04 +02:00
long LineEnd = 0;
2021-09-14 00:13:48 +02:00
std::vector<cmListFileArgument> Arguments;
};
std::shared_ptr<Implementation const> Impl;
};
2022-03-29 21:10:50 +02:00
class cmListFileContext
{
public:
std::string Name;
std::string FilePath;
long Line = 0;
static long const DeferPlaceholderLine = -1;
cm::optional<std::string> DeferId;
cmListFileContext() = default;
2022-08-04 22:12:04 +02:00
// This move constructor is marked `noexcept` yet `clang-tidy` 14 reports it
// as being able to throw an exception. Suppress the warning as there doesn't
// seem to be any way for this to happen given the member types.
// NOLINTNEXTLINE(bugprone-exception-escape)
cmListFileContext(cmListFileContext&& /*other*/) noexcept = default;
2022-03-29 21:10:50 +02:00
cmListFileContext(const cmListFileContext& /*other*/) = default;
cmListFileContext& operator=(const cmListFileContext& /*other*/) = default;
#if __cplusplus >= 201703L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L)
2022-08-04 22:12:04 +02:00
cmListFileContext& operator=(cmListFileContext&& /*other*/) noexcept =
default;
2022-03-29 21:10:50 +02:00
#else
// The move assignment operators for several STL classes did not become
// noexcept until C++17, which causes some tools to warn about this move
// assignment operator throwing an exception when it shouldn't.
2022-08-04 22:12:04 +02:00
cmListFileContext& operator=(cmListFileContext&& /*other*/) noexcept =
delete;
2022-03-29 21:10:50 +02:00
#endif
cmListFileContext(std::string name, std::string filePath, long line)
: Name(std::move(name))
, FilePath(std::move(filePath))
, Line(line)
{
}
2022-08-04 22:12:04 +02:00
static cmListFileContext FromListFilePath(std::string const& filePath)
{
// We are entering a file-level scope but have not yet reached
// any specific line or command invocation within it. This context
// is useful to print when it is at the top but otherwise can be
// skipped during call stack printing.
cmListFileContext lfc;
lfc.FilePath = filePath;
return lfc;
}
2022-03-29 21:10:50 +02:00
static cmListFileContext FromListFileFunction(
cmListFileFunction const& lff, std::string const& fileName,
cm::optional<std::string> deferId = {})
{
cmListFileContext lfc;
lfc.FilePath = fileName;
lfc.Line = lff.Line();
lfc.Name = lff.OriginalName();
lfc.DeferId = std::move(deferId);
return lfc;
}
};
std::ostream& operator<<(std::ostream&, cmListFileContext const&);
bool operator<(const cmListFileContext& lhs, const cmListFileContext& rhs);
bool operator==(cmListFileContext const& lhs, cmListFileContext const& rhs);
bool operator!=(cmListFileContext const& lhs, cmListFileContext const& rhs);
2022-08-04 22:12:04 +02:00
// Represent a backtrace (call stack) with efficient value semantics.
2015-11-17 17:22:37 +01:00
class cmListFileBacktrace
2022-08-04 22:12:04 +02:00
: public cmConstStack<cmListFileContext, cmListFileBacktrace>
2015-04-27 22:25:09 +02:00
{
2022-08-04 22:12:04 +02:00
using cmConstStack::cmConstStack;
friend class cmConstStack<cmListFileContext, cmListFileBacktrace>;
2015-04-27 22:25:09 +02:00
};
2022-08-04 22:12:04 +02:00
#ifndef cmListFileCache_cxx
extern template class cmConstStack<cmListFileContext, cmListFileBacktrace>;
#endif
2019-11-11 23:01:05 +01:00
// Wrap type T as a value with a backtrace. For purposes of
// ordering and equality comparison, only the original value is
// used. The backtrace is considered incidental.
template <typename T>
class BT
{
public:
BT(T v = T(), cmListFileBacktrace bt = cmListFileBacktrace())
: Value(std::move(v))
, Backtrace(std::move(bt))
{
}
T Value;
cmListFileBacktrace Backtrace;
friend bool operator==(BT<T> const& l, BT<T> const& r)
{
return l.Value == r.Value;
}
friend bool operator<(BT<T> const& l, BT<T> const& r)
{
return l.Value < r.Value;
}
friend bool operator==(BT<T> const& l, T const& r) { return l.Value == r; }
friend bool operator==(T const& l, BT<T> const& r) { return l == r.Value; }
};
std::ostream& operator<<(std::ostream& os, BT<std::string> const& s);
2021-09-14 00:13:48 +02:00
// Wrap type T as a value with potentially multiple backtraces. For purposes
// of ordering and equality comparison, only the original value is used. The
// backtrace is considered incidental.
template <typename T>
class BTs
{
public:
BTs(T v = T(), cmListFileBacktrace bt = cmListFileBacktrace())
: Value(std::move(v))
{
this->Backtraces.emplace_back(std::move(bt));
}
T Value;
std::vector<cmListFileBacktrace> Backtraces;
friend bool operator==(BTs<T> const& l, BTs<T> const& r)
{
return l.Value == r.Value;
}
friend bool operator<(BTs<T> const& l, BTs<T> const& r)
{
return l.Value < r.Value;
}
friend bool operator==(BTs<T> const& l, T const& r) { return l.Value == r; }
friend bool operator==(T const& l, BTs<T> const& r) { return l == r.Value; }
};
2022-08-04 22:12:04 +02:00
std::vector<BT<std::string>> cmExpandListWithBacktrace(
2019-11-11 23:01:05 +01:00
std::string const& list,
2022-08-04 22:12:04 +02:00
cmListFileBacktrace const& bt = cmListFileBacktrace(),
2023-07-02 19:51:09 +02:00
cmList::EmptyElements emptyArgs = cmList::EmptyElements::No);
2019-11-11 23:01:05 +01:00
struct cmListFile
{
2016-10-30 18:24:19 +01:00
bool ParseFile(const char* path, cmMessenger* messenger,
cmListFileBacktrace const& lfbt);
2020-08-30 11:54:41 +02:00
bool ParseString(const char* str, const char* virtual_filename,
cmMessenger* messenger, cmListFileBacktrace const& lfbt);
std::vector<cmListFileFunction> Functions;
};