From c9b0c805599f6801481d35a1bfd8ab5d9ffa0716 Mon Sep 17 00:00:00 2001 From: Jooyung Han Date: Thu, 31 Dec 2020 11:50:21 +0900 Subject: add -Wout-array Methods having array output parameters, like void foo(out String[] ret) are usually bad because the output array size must be declared and allocated by the client in Java, and so the size of the array output cannot be chosen by the server. This undesirable behavior happens because of how arrays work in Java (they cannot be reallocated). Instead prefer APIs like String[] foo(). Bug: 168028537 Test: aidl_unittests Change-Id: Iccee9cf2cb9031f19b45972dadbaaa22670e09b4 --- diagnostics.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'diagnostics.cpp') diff --git a/diagnostics.cpp b/diagnostics.cpp index 4af90331..9a4cfb8d 100644 --- a/diagnostics.cpp +++ b/diagnostics.cpp @@ -224,6 +224,20 @@ struct DiagnoseMixedOneway : DiagnosticsVisitor { } }; +struct DiagnoseOutArray : DiagnosticsVisitor { + DiagnoseOutArray(DiagnosticsContext& diag) : DiagnosticsVisitor(diag) {} + void Visit(const AidlMethod& m) override { + for (const auto& a : m.GetArguments()) { + if (a->GetType().IsArray() && a->IsOut()) { + diag.Report(m.GetLocation(), DiagnosticID::out_array) + << "The method '" << m.GetName() << "' an array output parameter '" << a->GetName() + << "'. Instead prefer APIs like '" << a->GetType().Signature() << " " << m.GetName() + << "(...)."; + } + } + } +}; + bool Diagnose(const AidlDocument& doc, const DiagnosticMapping& mapping) { DiagnosticsContext diag(mapping); @@ -233,6 +247,7 @@ bool Diagnose(const AidlDocument& doc, const DiagnosticMapping& mapping) { DiagnoseConstName{diag}.Check(doc); DiagnoseExplicitDefault{diag}.Check(doc); DiagnoseMixedOneway{diag}.Check(doc); + DiagnoseOutArray{diag}.Check(doc); return diag.ErrorCount() == 0; } -- cgit v1.2.3