1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-22 02:33:06 +01:00

Add -Wno-error=unknown flag to clang-format.

Currently newer clang-format options cannot be included in .clang-format files, if not all users can be forced to use an updated version.
This patch tries to solve this by adding an option to clang-format, enabling to ignore unknown (newer) options.

Differential Revision: https://reviews.llvm.org/D86137
This commit is contained in:
Joachim Meyer 2020-08-18 14:33:34 +02:00
parent cba5d6bed8
commit d06a70e02e
5 changed files with 47 additions and 9 deletions

View File

@ -40,6 +40,7 @@
#include "llvm/ADT/StringRef.h" #include "llvm/ADT/StringRef.h"
#include "llvm/Support/Allocator.h" #include "llvm/Support/Allocator.h"
#include "llvm/Support/SMLoc.h" #include "llvm/Support/SMLoc.h"
#include "llvm/Support/SourceMgr.h"
#include <cassert> #include <cassert>
#include <cstddef> #include <cstddef>
#include <iterator> #include <iterator>
@ -51,7 +52,6 @@
namespace llvm { namespace llvm {
class MemoryBufferRef; class MemoryBufferRef;
class SourceMgr;
class raw_ostream; class raw_ostream;
class Twine; class Twine;
@ -100,7 +100,8 @@ public:
return !failed(); return !failed();
} }
void printError(Node *N, const Twine &Msg); void printError(Node *N, const Twine &Msg,
SourceMgr::DiagKind Kind = SourceMgr::DK_Error);
private: private:
friend class Document; friend class Document;

View File

@ -789,6 +789,7 @@ public:
virtual NodeKind getNodeKind() = 0; virtual NodeKind getNodeKind() = 0;
virtual void setError(const Twine &) = 0; virtual void setError(const Twine &) = 0;
virtual void setAllowUnknownKeys(bool Allow);
template <typename T> template <typename T>
void enumCase(T &Val, const char* Str, const T ConstVal) { void enumCase(T &Val, const char* Str, const T ConstVal) {
@ -1495,6 +1496,9 @@ private:
void setError(HNode *hnode, const Twine &message); void setError(HNode *hnode, const Twine &message);
void setError(Node *node, const Twine &message); void setError(Node *node, const Twine &message);
void reportWarning(HNode *hnode, const Twine &message);
void reportWarning(Node *hnode, const Twine &message);
public: public:
// These are only used by operator>>. They could be private // These are only used by operator>>. They could be private
// if those templated things could be made friends. // if those templated things could be made friends.
@ -1504,6 +1508,8 @@ public:
/// Returns the current node that's being parsed by the YAML Parser. /// Returns the current node that's being parsed by the YAML Parser.
const Node *getCurrentNode() const; const Node *getCurrentNode() const;
void setAllowUnknownKeys(bool Allow) override;
private: private:
SourceMgr SrcMgr; // must be before Strm SourceMgr SrcMgr; // must be before Strm
std::unique_ptr<llvm::yaml::Stream> Strm; std::unique_ptr<llvm::yaml::Stream> Strm;
@ -1514,6 +1520,7 @@ private:
std::vector<bool> BitValuesUsed; std::vector<bool> BitValuesUsed;
HNode *CurrentNode = nullptr; HNode *CurrentNode = nullptr;
bool ScalarMatchFound = false; bool ScalarMatchFound = false;
bool AllowUnknownKeys = false;
}; };
/// ///

View File

@ -1775,12 +1775,9 @@ Stream::~Stream() = default;
bool Stream::failed() { return scanner->failed(); } bool Stream::failed() { return scanner->failed(); }
void Stream::printError(Node *N, const Twine &Msg) { void Stream::printError(Node *N, const Twine &Msg, SourceMgr::DiagKind Kind) {
SMRange Range = N ? N->getSourceRange() : SMRange(); SMRange Range = N ? N->getSourceRange() : SMRange();
scanner->printError( Range.Start scanner->printError(Range.Start, Kind, Msg, Range);
, SourceMgr::DK_Error
, Msg
, Range);
} }
document_iterator Stream::begin() { document_iterator Stream::begin() {

View File

@ -48,6 +48,10 @@ void IO::setContext(void *Context) {
Ctxt = Context; Ctxt = Context;
} }
void IO::setAllowUnknownKeys(bool Allow) {
llvm_unreachable("Only supported for Input");
}
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
// Input // Input
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
@ -197,8 +201,12 @@ void Input::endMapping() {
return; return;
for (const auto &NN : MN->Mapping) { for (const auto &NN : MN->Mapping) {
if (!is_contained(MN->ValidKeys, NN.first())) { if (!is_contained(MN->ValidKeys, NN.first())) {
setError(NN.second.get(), Twine("unknown key '") + NN.first() + "'"); HNode *ReportNode = NN.second.get();
if (!AllowUnknownKeys) {
setError(ReportNode, Twine("unknown key '") + NN.first() + "'");
break; break;
} else
reportWarning(ReportNode, Twine("unknown key '") + NN.first() + "'");
} }
} }
} }
@ -370,6 +378,11 @@ void Input::setError(Node *node, const Twine &message) {
EC = make_error_code(errc::invalid_argument); EC = make_error_code(errc::invalid_argument);
} }
void Input::reportWarning(HNode *hnode, const Twine &message) {
assert(hnode && "HNode must not be NULL");
Strm->printError(hnode->_node, message, SourceMgr::DK_Warning);
}
std::unique_ptr<Input::HNode> Input::createHNodes(Node *N) { std::unique_ptr<Input::HNode> Input::createHNodes(Node *N) {
SmallString<128> StringStorage; SmallString<128> StringStorage;
if (ScalarNode *SN = dyn_cast<ScalarNode>(N)) { if (ScalarNode *SN = dyn_cast<ScalarNode>(N)) {
@ -428,6 +441,8 @@ void Input::setError(const Twine &Message) {
setError(CurrentNode, Message); setError(CurrentNode, Message);
} }
void Input::setAllowUnknownKeys(bool Allow) { AllowUnknownKeys = Allow; }
bool Input::canElideEmptySequence() { bool Input::canElideEmptySequence() {
return false; return false;
} }

View File

@ -35,3 +35,21 @@ TEST(ObjectYAML, BinaryRef) {
YOut << BH; YOut << BH;
EXPECT_NE(OS.str().find("''"), StringRef::npos); EXPECT_NE(OS.str().find("''"), StringRef::npos);
} }
TEST(ObjectYAML, UnknownOption) {
StringRef InputYAML = "InvalidKey: InvalidValue\n"
"Binary: AAAA\n";
BinaryHolder BH;
yaml::Input Input(InputYAML);
// test 1: default in trying to parse invalid key is an error case.
Input >> BH;
EXPECT_EQ(Input.error().value(), 22);
// test 2: only warn about invalid key if actively set.
yaml::Input Input2(InputYAML);
BinaryHolder BH2;
Input2.setAllowUnknownKeys(true);
Input2 >> BH2;
EXPECT_EQ(BH2.Binary, yaml::BinaryRef("AAAA"));
EXPECT_EQ(Input2.error().value(), 0);
}