forked from p4lang/p4c
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve
BUG_CHECK
internals (p4lang#4678)
* Refactor slightly bug_helper and corresponding code: - Ensure we are using forwarding references and do not pass things by value - Use std::string instead of cstring to hold full message inside exception, cstring does not make any sense here - Switch to std::string_view to capture string arguments instead of std::string passed by value * Use abseil string library to simplify the code * No need to special case `big_int`, it will be handled by the generic template code * Get rid of `message` argument, it is always empty * Unbreak bazel build * Address review comments
- Loading branch information
Showing
7 changed files
with
183 additions
and
183 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
/* | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
/* -*-c++-*- */ | ||
|
||
#ifndef LIB_BUG_HELPER_H_ | ||
#define LIB_BUG_HELPER_H_ | ||
|
||
#include <string> | ||
#include <string_view> | ||
|
||
#include <boost/format.hpp> | ||
|
||
#include "absl/strings/str_cat.h" | ||
#include "lib/cstring.h" | ||
#include "lib/source_file.h" | ||
|
||
template <class... Args> | ||
std::string bug_helper(boost::format &f, std::string_view position, std::string_view tail, | ||
const char *t, Args &&...args); | ||
|
||
template <class... Args> | ||
std::string bug_helper(boost::format &f, std::string_view position, std::string_view tail, | ||
const cstring &t, Args &&...args); | ||
|
||
template <class... Args> | ||
std::string bug_helper(boost::format &f, std::string_view position, std::string_view tail, | ||
const Util::SourceInfo &info, Args &&...args); | ||
|
||
template <typename T, class... Args> | ||
auto bug_helper(boost::format &f, std::string_view position, std::string_view tail, const T &t, | ||
Args &&...args) | ||
-> std::enable_if_t<std::is_base_of_v<Util::IHasSourceInfo, T>, std::string>; | ||
|
||
template <typename T, class... Args> | ||
auto bug_helper(boost::format &f, std::string_view position, std::string_view tail, const T &t, | ||
Args &&...args) | ||
-> std::enable_if_t<!std::is_base_of_v<Util::IHasSourceInfo, T>, std::string>; | ||
|
||
template <typename T, class... Args> | ||
auto bug_helper(boost::format &f, std::string_view position, std::string_view tail, const T *t, | ||
Args &&...args) | ||
-> std::enable_if_t<std::is_base_of_v<Util::IHasSourceInfo, T>, std::string>; | ||
|
||
template <typename T, class... Args> | ||
auto bug_helper(boost::format &f, std::string_view position, std::string_view tail, const T *t, | ||
Args &&...args) | ||
-> std::enable_if_t<!std::is_base_of_v<Util::IHasSourceInfo, T>, std::string>; | ||
|
||
// actual implementations | ||
namespace detail { | ||
static inline auto getPositionTail(const Util::SourceInfo &info, std::string_view position, | ||
std::string_view tail) { | ||
std::string_view posString = info.toPositionString().string_view(); | ||
std::string outTail(tail); | ||
if (position.empty()) { | ||
position = posString; | ||
} else { | ||
outTail.append(posString); | ||
if (!posString.empty()) outTail.append("\n"); | ||
} | ||
outTail += info.toSourceFragment(); | ||
|
||
return std::pair(position, outTail); | ||
} | ||
} // namespace detail | ||
|
||
static inline std::string bug_helper(boost::format &f, std::string_view position, | ||
std::string_view tail) { | ||
return absl::StrCat(position, position.empty() ? "" : ": ", boost::str(f), "\n", tail); | ||
} | ||
|
||
template <class... Args> | ||
std::string bug_helper(boost::format &f, std::string_view position, std::string_view tail, | ||
const char *t, Args &&...args) { | ||
return bug_helper(f % t, position, tail, std::forward<Args>(args)...); | ||
} | ||
|
||
template <class... Args> | ||
std::string bug_helper(boost::format &f, std::string_view position, std::string_view tail, | ||
const cstring &t, Args &&...args) { | ||
return bug_helper(f % t.string_view(), position, tail, std::forward<Args>(args)...); | ||
} | ||
|
||
template <typename T, class... Args> | ||
auto bug_helper(boost::format &f, std::string_view position, std::string_view tail, const T *t, | ||
Args &&...args) | ||
-> std::enable_if_t<!std::is_base_of_v<Util::IHasSourceInfo, T>, std::string> { | ||
std::stringstream str; | ||
str << t; | ||
return bug_helper(f % str.str(), position, tail, std::forward<Args>(args)...); | ||
} | ||
|
||
template <typename T, class... Args> | ||
auto bug_helper(boost::format &f, std::string_view position, std::string_view tail, const T &t, | ||
Args &&...args) | ||
-> std::enable_if_t<!std::is_base_of_v<Util::IHasSourceInfo, T>, std::string> { | ||
return bug_helper(f % t, position, tail, std::forward<Args>(args)...); | ||
} | ||
|
||
template <class... Args> | ||
std::string bug_helper(boost::format &f, std::string_view position, std::string_view tail, | ||
const Util::SourceInfo &info, Args &&...args) { | ||
auto [outPos, outTail] = detail::getPositionTail(info, position, tail); | ||
return bug_helper(f % "", outPos, outTail, std::forward<Args>(args)...); | ||
} | ||
|
||
template <typename T, class... Args> | ||
auto bug_helper(boost::format &f, std::string_view position, std::string_view tail, const T *t, | ||
Args &&...args) | ||
-> std::enable_if_t<std::is_base_of_v<Util::IHasSourceInfo, T>, std::string> { | ||
if (t == nullptr) return bug_helper(f, position, tail, std::forward<Args>(args)...); | ||
|
||
auto [outPos, outTail] = detail::getPositionTail(t->getSourceInfo(), position, tail); | ||
std::stringstream str; | ||
str << t; | ||
return bug_helper(f % str.str(), outPos, outTail, std::forward<Args>(args)...); | ||
} | ||
|
||
template <typename T, class... Args> | ||
auto bug_helper(boost::format &f, std::string_view position, std::string_view tail, const T &t, | ||
Args &&...args) | ||
-> std::enable_if_t<std::is_base_of_v<Util::IHasSourceInfo, T>, std::string> { | ||
auto [outPos, outTail] = detail::getPositionTail(t.getSourceInfo(), position, tail); | ||
|
||
std::stringstream str; | ||
str << t; | ||
return bug_helper(f % str.str(), outPos, outTail, std::forward<Args>(args)...); | ||
} | ||
|
||
#endif /* LIB_BUG_HELPER_H_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.