From a735d6eae2847b039d12c75d4c794862dad59bc1 Mon Sep 17 00:00:00 2001 From: Simon Pilgrim Date: Sun, 20 Sep 2020 13:55:04 +0100 Subject: [PATCH] JSONCompilationDatabase.cpp - cleanup key parsing error checks. NFCI. Merge the key + sequence/value checks with the key handling code. Reduces the number of key string comparisons and avoids a number of clang static analyzer null dereference warnings. --- clang/lib/Tooling/JSONCompilationDatabase.cpp | 43 ++++++++++++++------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/clang/lib/Tooling/JSONCompilationDatabase.cpp b/clang/lib/Tooling/JSONCompilationDatabase.cpp index 4af361f..67a42d3 100644 --- a/clang/lib/Tooling/JSONCompilationDatabase.cpp +++ b/clang/lib/Tooling/JSONCompilationDatabase.cpp @@ -369,16 +369,11 @@ bool JSONCompilationDatabase::parse(std::string &ErrorMessage) { } auto *ValueString = dyn_cast(Value); auto *SequenceString = dyn_cast(Value); - if (KeyValue == "arguments" && !SequenceString) { - ErrorMessage = "Expected sequence as value."; - return false; - } else if (KeyValue != "arguments" && !ValueString) { - ErrorMessage = "Expected string as value."; - return false; - } - if (KeyValue == "directory") { - Directory = ValueString; - } else if (KeyValue == "arguments") { + if (KeyValue == "arguments") { + if (!SequenceString) { + ErrorMessage = "Expected sequence as value."; + return false; + } Command = std::vector(); for (auto &Argument : *SequenceString) { auto *Scalar = dyn_cast(&Argument); @@ -388,17 +383,25 @@ bool JSONCompilationDatabase::parse(std::string &ErrorMessage) { } Command->push_back(Scalar); } - } else if (KeyValue == "command") { - if (!Command) - Command = std::vector(1, ValueString); - } else if (KeyValue == "file") { - File = ValueString; - } else if (KeyValue == "output") { - Output = ValueString; } else { - ErrorMessage = ("Unknown key: \"" + - KeyString->getRawValue() + "\"").str(); - return false; + if (!ValueString) { + ErrorMessage = "Expected string as value."; + return false; + } + if (KeyValue == "directory") { + Directory = ValueString; + } else if (KeyValue == "command") { + if (!Command) + Command = std::vector(1, ValueString); + } else if (KeyValue == "file") { + File = ValueString; + } else if (KeyValue == "output") { + Output = ValueString; + } else { + ErrorMessage = + ("Unknown key: \"" + KeyString->getRawValue() + "\"").str(); + return false; + } } } if (!File) { -- 2.7.4