Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d055e38c authored by Brandon Liu's avatar Brandon Liu Committed by Android (Google) Code Review
Browse files

Merge "Adding verbose setters and getters for diagnostics to make...

Merge "Adding verbose setters and getters for diagnostics to make ResourceUtils code able to print verbose message."
parents cdb6e2af 48d229de
Loading
Loading
Loading
Loading
+19 −0
Original line number Original line Diff line number Diff line
@@ -86,6 +86,17 @@ struct IDiagnostics {
    DiagMessageActual actual = message.Build();
    DiagMessageActual actual = message.Build();
    Log(Level::Note, actual);
    Log(Level::Note, actual);
  }
  }

  virtual void SetVerbose(bool val) {
    verbose_ = val;
  }

  virtual bool IsVerbose() {
    return verbose_;
  }

  private:
    bool verbose_ = false;
};
};


class SourcePathDiagnostics : public IDiagnostics {
class SourcePathDiagnostics : public IDiagnostics {
@@ -105,6 +116,14 @@ class SourcePathDiagnostics : public IDiagnostics {
    return error;
    return error;
  }
  }


  void SetVerbose(bool val) override {
    diag_->SetVerbose(val);
  }

  bool IsVerbose() override {
    return diag_->IsVerbose();
  }

 private:
 private:
  Source source_;
  Source source_;
  IDiagnostics* diag_;
  IDiagnostics* diag_;
+1 −1
Original line number Original line Diff line number Diff line
@@ -800,7 +800,7 @@ std::unique_ptr<Item> ResourceParser::ParseXml(const FlattenedXmlSubTree& xmlsub


  // Process the raw value.
  // Process the raw value.
  std::unique_ptr<Item> processed_item = ResourceUtils::TryParseItemForAttribute(
  std::unique_ptr<Item> processed_item = ResourceUtils::TryParseItemForAttribute(
      xmlsub_tree.raw_value, type_mask, on_create_reference);
      &diag, xmlsub_tree.raw_value, type_mask, on_create_reference);
  if (processed_item) {
  if (processed_item) {
    // Fix up the reference.
    // Fix up the reference.
    if (auto ref = ValueCast<Reference>(processed_item.get())) {
    if (auto ref = ValueCast<Reference>(processed_item.get())) {
+9 −3
Original line number Original line Diff line number Diff line
@@ -619,7 +619,7 @@ uint32_t AndroidTypeToAttributeTypeMask(uint16_t type) {
}
}


std::unique_ptr<Item> TryParseItemForAttribute(
std::unique_ptr<Item> TryParseItemForAttribute(
    StringPiece value, uint32_t type_mask,
    android::IDiagnostics* diag, StringPiece value, uint32_t type_mask,
    const std::function<bool(const ResourceName&)>& on_create_reference) {
    const std::function<bool(const ResourceName&)>& on_create_reference) {
  using android::ResTable_map;
  using android::ResTable_map;


@@ -685,6 +685,12 @@ std::unique_ptr<Item> TryParseItemForAttribute(
            // same string is smaller than 1, otherwise return as raw string.
            // same string is smaller than 1, otherwise return as raw string.
            if (fabs(f - d) < 1) {
            if (fabs(f - d) < 1) {
              return std::move(floating_point);
              return std::move(floating_point);
            } else {
              if (diag->IsVerbose()) {
                diag->Note(android::DiagMessage()
                           << "precision lost greater than 1 while parsing float " << value
                           << ", return a raw string");
              }
            }
            }
          }
          }
        } else {
        } else {
@@ -701,12 +707,12 @@ std::unique_ptr<Item> TryParseItemForAttribute(
 * allows.
 * allows.
 */
 */
std::unique_ptr<Item> TryParseItemForAttribute(
std::unique_ptr<Item> TryParseItemForAttribute(
    StringPiece str, const Attribute* attr,
    android::IDiagnostics* diag, StringPiece str, const Attribute* attr,
    const std::function<bool(const ResourceName&)>& on_create_reference) {
    const std::function<bool(const ResourceName&)>& on_create_reference) {
  using android::ResTable_map;
  using android::ResTable_map;


  const uint32_t type_mask = attr->type_mask;
  const uint32_t type_mask = attr->type_mask;
  auto value = TryParseItemForAttribute(str, type_mask, on_create_reference);
  auto value = TryParseItemForAttribute(diag, str, type_mask, on_create_reference);
  if (value) {
  if (value) {
    return value;
    return value;
  }
  }
+2 −2
Original line number Original line Diff line number Diff line
@@ -200,11 +200,11 @@ std::unique_ptr<BinaryPrimitive> TryParseFlagSymbol(const Attribute* enum_attr,
 * reference to an ID that must be created (@+id/foo).
 * reference to an ID that must be created (@+id/foo).
 */
 */
std::unique_ptr<Item> TryParseItemForAttribute(
std::unique_ptr<Item> TryParseItemForAttribute(
    android::StringPiece value, const Attribute* attr,
    android::IDiagnostics* diag, android::StringPiece value, const Attribute* attr,
    const std::function<bool(const ResourceName&)>& on_create_reference = {});
    const std::function<bool(const ResourceName&)>& on_create_reference = {});


std::unique_ptr<Item> TryParseItemForAttribute(
std::unique_ptr<Item> TryParseItemForAttribute(
    android::StringPiece value, uint32_t type_mask,
    android::IDiagnostics* diag, android::StringPiece value, uint32_t type_mask,
    const std::function<bool(const ResourceName&)>& on_create_reference = {});
    const std::function<bool(const ResourceName&)>& on_create_reference = {});


uint32_t AndroidTypeToAttributeTypeMask(uint16_t type);
uint32_t AndroidTypeToAttributeTypeMask(uint16_t type);
+16 −7
Original line number Original line Diff line number Diff line
@@ -217,34 +217,43 @@ TEST(ResourceUtilsTest, EmptyIsBinaryPrimitive) {
}
}


TEST(ResourceUtilsTest, ItemsWithWhitespaceAreParsedCorrectly) {
TEST(ResourceUtilsTest, ItemsWithWhitespaceAreParsedCorrectly) {
  EXPECT_THAT(ResourceUtils::TryParseItemForAttribute(" 12\n   ", ResTable_map::TYPE_INTEGER),
  std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
  EXPECT_THAT(ResourceUtils::TryParseItemForAttribute(context->GetDiagnostics(), " 12\n   ",
                                                      ResTable_map::TYPE_INTEGER),
              Pointee(ValueEq(BinaryPrimitive(Res_value::TYPE_INT_DEC, 12u))));
              Pointee(ValueEq(BinaryPrimitive(Res_value::TYPE_INT_DEC, 12u))));
  EXPECT_THAT(ResourceUtils::TryParseItemForAttribute(" true\n   ", ResTable_map::TYPE_BOOLEAN),
  EXPECT_THAT(ResourceUtils::TryParseItemForAttribute(context->GetDiagnostics(), " true\n   ",
                                                      ResTable_map::TYPE_BOOLEAN),
              Pointee(ValueEq(BinaryPrimitive(Res_value::TYPE_INT_BOOLEAN, 0xffffffffu))));
              Pointee(ValueEq(BinaryPrimitive(Res_value::TYPE_INT_BOOLEAN, 0xffffffffu))));


  const float expected_float = 12.0f;
  const float expected_float = 12.0f;
  const uint32_t expected_float_flattened = *(uint32_t*)&expected_float;
  const uint32_t expected_float_flattened = *(uint32_t*)&expected_float;
  EXPECT_THAT(ResourceUtils::TryParseItemForAttribute(" 12.0\n   ", ResTable_map::TYPE_FLOAT),
  EXPECT_THAT(ResourceUtils::TryParseItemForAttribute(context->GetDiagnostics(), " 12.0\n   ",
                                                      ResTable_map::TYPE_FLOAT),
              Pointee(ValueEq(BinaryPrimitive(Res_value::TYPE_FLOAT, expected_float_flattened))));
              Pointee(ValueEq(BinaryPrimitive(Res_value::TYPE_FLOAT, expected_float_flattened))));
}
}


TEST(ResourceUtilsTest, FloatAndBigIntegerParsedCorrectly) {
TEST(ResourceUtilsTest, FloatAndBigIntegerParsedCorrectly) {
  std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
  const float expected_float = 0.125f;
  const float expected_float = 0.125f;
  const uint32_t expected_float_flattened = *(uint32_t*)&expected_float;
  const uint32_t expected_float_flattened = *(uint32_t*)&expected_float;
  EXPECT_THAT(ResourceUtils::TryParseItemForAttribute("0.125", ResTable_map::TYPE_FLOAT),
  EXPECT_THAT(ResourceUtils::TryParseItemForAttribute(context->GetDiagnostics(), "0.125",
                                                      ResTable_map::TYPE_FLOAT),
              Pointee(ValueEq(BinaryPrimitive(Res_value::TYPE_FLOAT, expected_float_flattened))));
              Pointee(ValueEq(BinaryPrimitive(Res_value::TYPE_FLOAT, expected_float_flattened))));


  const float special_float = 1.0f;
  const float special_float = 1.0f;
  const uint32_t special_float_flattened = *(uint32_t*)&special_float;
  const uint32_t special_float_flattened = *(uint32_t*)&special_float;
  EXPECT_THAT(ResourceUtils::TryParseItemForAttribute("1.0", ResTable_map::TYPE_FLOAT),
  EXPECT_THAT(ResourceUtils::TryParseItemForAttribute(context->GetDiagnostics(), "1.0",
                                                      ResTable_map::TYPE_FLOAT),
              Pointee(ValueEq(BinaryPrimitive(Res_value::TYPE_FLOAT, special_float_flattened))));
              Pointee(ValueEq(BinaryPrimitive(Res_value::TYPE_FLOAT, special_float_flattened))));


  EXPECT_EQ(ResourceUtils::TryParseItemForAttribute("1099511627776", ResTable_map::TYPE_INTEGER),
  EXPECT_EQ(ResourceUtils::TryParseItemForAttribute(context->GetDiagnostics(), "1099511627776",
                                                    ResTable_map::TYPE_INTEGER),
            std::unique_ptr<Item>(nullptr));
            std::unique_ptr<Item>(nullptr));


  const float big_float = 1099511627776.0f;
  const float big_float = 1099511627776.0f;
  const uint32_t big_flattened = *(uint32_t*)&big_float;
  const uint32_t big_flattened = *(uint32_t*)&big_float;
  EXPECT_THAT(ResourceUtils::TryParseItemForAttribute("1099511627776", ResTable_map::TYPE_FLOAT),
  EXPECT_THAT(ResourceUtils::TryParseItemForAttribute(context->GetDiagnostics(), "1099511627776",
                                                      ResTable_map::TYPE_FLOAT),
              Pointee(ValueEq(BinaryPrimitive(Res_value::TYPE_FLOAT, big_flattened))));
              Pointee(ValueEq(BinaryPrimitive(Res_value::TYPE_FLOAT, big_flattened))));
}
}


Loading