Cryptic chromium-style error

327 views
Skip to first unread message

Paul Miller

unread,
Jun 7, 2018, 3:38:32 PM6/7/18
to Chromium-dev, Alexei Svitkine, Bo Liu
This code:

struct Foo {
  std::string s1; 
  std::string s2; 
  std::string s3; 
  std::string s4; 
};

generates a compile error:

../../components/variations/seed_response.h:22:1: error: [chromium-style] Complex class/struct needs an explicit out-of-line constructor.
struct Foo {
^

However, each of these:

struct Foo {
  std::string s1; 
  std::string s2; 
  std::string s3; 
};

typedef struct {
  std::string s1; 
  std::string s2; 
  std::string s3; 
  std::string s4;
} Foo;

struct Foo {
  int i1;
  int i2;
  int i3;
  int i4;
};

compiles successfully.

Questions: what is a "complex class/struct", and why must we write constructors for them? I don't see anything about this in the style guide. And why does only the first example trigger the error?

Bo Liu

unread,
Jun 7, 2018, 3:46:37 PM6/7/18
to Paul Miller, chromium-dev, asvi...@google.com
(from right address..)

std::string is not a POD (plain old data) type, which means the constructor and destructor for your struct actually contains code. My understanding is if you don't write constructors/destructors, then the default generated ones are considered inline. If the struct is used widely, then that causes code bloat, due to many copies of inlined constructor/destructor.

The clang plugin is probably not meant to be bulletproof, just catching the most common use case.

--
--
Chromium Developers mailing list: chromi...@chromium.org
View archives, change email options, or unsubscribe:
http://groups.google.com/a/chromium.org/group/chromium-dev
---
You received this message because you are subscribed to the Google Groups "Chromium-dev" group.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-dev/CADsaz-kgBW1_656f%2BvJHfJu5W1SZRqVN6sfoFXiQK-bB-fz2pw%40mail.gmail.com.

Peter Boström

unread,
Jun 7, 2018, 4:02:38 PM6/7/18
to bo...@chromium.org, paulm...@google.com, Chromium-dev, Alexei Svitkine
Allegedly this didn't trigger for 3 non-POD std::strings. Is that threshold arbitrary or somehow based on the complexity of the inlined constructors?

The "defaulted default" constructor (C++ is good at naming things) is useful here as it arguably doesn't really make you define a constructor, just move it.

In .h:

struct Foo {
  Foo();
  ..
}

In .cc:

Foo::Foo() = default;

Daniel Cheng

unread,
Jun 7, 2018, 4:08:58 PM6/7/18
to pb...@chromium.org, bo...@chromium.org, Paul Miller, Chromium-dev, Alexei Svitkine

John Rummell

unread,
Jun 7, 2018, 4:23:35 PM6/7/18
to dch...@chromium.org, pb...@chromium.org, bo...@chromium.org, Paul Miller, Chromium-dev, Alexei Svitkine
https://www.chromium.org/developers/coding-style/chromium-style-checker-errors documents this (as I've run into it before).

"Internally, the plugin determines whether a class is complex using a point system, adding 9 points for each templated base class, 10 for each templated member variable, 3 for each non-POD type member variable, and (only for the constructor score) 1 for each integral data type. If a class's score is greater than or equal to 10, it will trip these errors."

Peter Kasting

unread,
Jun 7, 2018, 4:31:13 PM6/7/18
to Paul Miller, Chromium-dev, Alexei Svitkine, Bo Liu
Beyond what others noted, there's a little bit related to this at https://www.chromium.org/developers/coding-style/cpp-dos-and-donts#TOC-Stop-inlining-constructors-and-destructors , as well as the sections above and below.

PK

Paul Miller

unread,
Jun 7, 2018, 5:48:41 PM6/7/18
to Alexei Svitkine, pkas...@chromium.org, Chromium-dev, bo...@chromium.org
+1 to a better error message ^

Is the typedef thing just a bug in the plugin, then?

On Thu, Jun 7, 2018 at 2:42 PM Alexei Svitkine <asvi...@google.com> wrote:
Could the error from the clang plugin be made to point to the docs?
Reply all
Reply to author
Forward
0 new messages