From: Simon Pilgrim Date: Sun, 20 Sep 2020 12:55:04 +0000 (+0100) Subject: JSONCompilationDatabase.cpp - cleanup key parsing error checks. NFCI. X-Git-Tag: llvmorg-13-init~11511 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=a735d6eae2847b039d12c75d4c794862dad59bc1;p=platform%2Fupstream%2Fllvm.git 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. --- 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) {