8000 [TESTING] Remove use of deprecated C++ features, allow compilation with C++20 by vlstill · Pull Request #4297 · p4lang/p4c · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

[TESTING] Remove use of deprecated C++ features, allow compilation with C++20 #4297

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
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
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ endif()
project (P4C)

# set (CMAKE_CXX_EXTENSIONS OFF) # prefer using -std=c++17 rather than -std=gnu++17
set (CMAKE_CXX_STANDARD 17)
set (CMAKE_CXX_STANDARD 20)
set (CMAKE_CXX_STANDARD_REQUIRED ON)

set (CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
Expand Down
22 changes: 18 additions & 4 deletions lib/cstring.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,21 +91,21 @@ class cstring {
// Owner of string is someone else, we do not know size of string.
// Do not use if possible, this is linear time operation if string
// not exists in table, because the underlying string must be copied.
cstring(const char *string) { // NOLINT(runtime/explicit)
explicit cstring(const char *string) {
if (string != nullptr) {
construct_from_shared(string, std::strlen(string));
}
}

// construct cstring from std::string. Do not use if possible, this is linear
// time operation if string not exists in table, because the underlying string must be copied.
cstring(const std::string &string) { // NOLINT(runtime/explicit)
explicit cstring(const std::string &string) {
construct_from_shared(string.data(), string.length());
}

// construct cstring from std::string_view. Do not use if possible, this is linear
// time operation if string not exists in table, because the underlying string must be copied.
explicit cstring(std::string_view string) { // NOLINT(runtime/explicit)
explicit cstring(std::string_view string) {
construct_from_shared(string.data(), string.length());
}

Expand All @@ -114,9 +114,19 @@ class cstring {
// Just helper function, for lazies, who do not like to write .str()
// Do not use it, implicit std::string construction with implicit overhead
// TODO (DanilLutsenko): Remove it?
cstring(const std::stringstream &stream) // NOLINT(runtime/explicit)
explicit cstring(const std::stringstream &stream)
: cstring(stream.str()) {}

cstring(const cstring &) = default;
cstring(cstring &&) = default;

cstring &operator=(const cstring &) = default;
cstring &operator=(cstring &&) = default;
cstring &operator=(const std::string &str) {
construct_from_shared(str.data(), str.length());
return *this;
}

// TODO (DanilLutsenko): Construct from StringRef?

// String was created outside and cstring is unique owner of it.
Expand Down Expand Up @@ -163,6 +173,7 @@ class cstring {

char get(unsigned index) const { return (index < size()) ? str[index] : 0; }
const char *c_str() const { return str; }
const char *data() const { return str; }
operator const char *() const { return str; }

// Size tests. Constant time except for size(), which is linear time.
Expand Down Expand Up @@ -209,6 +220,9 @@ class cstring {
bool operator>(const std::string &a) const { return *this > a.c_str(); }
bool operator>=(const std::string &a) const { return *this >= a.c_str(); }

/// implicit cast to std::string_view, similar to what std::string has
operator std::string_view() const { return std::string_view(data(), size()); }

bool startsWith(const cstring &prefix) const;
bool endsWith(const cstring &suffix) const;

Expand Down
2 changes: 1 addition & 1 deletion lib/error_catalog.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ class ErrorCatalog {
/// retrieve the name for errorCode
cstring getName(int errorCode) {
if (errorCatalog.count(errorCode)) return errorCatalog.at(errorCode);
return "--unknown--";
return cstring("--unknown--");
}

/// return true if the given diagnostic can _only_ be an error; false otherwise
Expand Down
0