Skip to content

Commit

Permalink
Add flags to configure basic format styles.
Browse files Browse the repository at this point in the history
Fixes chipsalliance#1046

Signed-off-by: Henner Zeller <[email protected]>
  • Loading branch information
hzeller committed Nov 3, 2021
1 parent a5d3c7e commit 858aa94
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 2 deletions.
11 changes: 11 additions & 0 deletions common/formatting/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,17 @@ cc_library(
],
)

cc_library(
name = "basic_format_style_init",
srcs = ["basic_format_style_init.cc"],
hdrs = ["basic_format_style_init.h"],
deps = [
":basic_format_style",
"@com_google_absl//absl/flags:flag",
"@com_google_absl//absl/strings",
],
)

cc_test(
name = "basic_format_style_test",
srcs = ["basic_format_style_test.cc"],
Expand Down
2 changes: 2 additions & 0 deletions common/formatting/basic_format_style.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ struct BasicFormatStyle {

// Penalty added to solution for each introduced line break.
int line_break_penalty = 2;

// -- Note: when adding new fields, add them in basic_format_style_init.cc
};

// Control how a section of code is indented.
Expand Down
52 changes: 52 additions & 0 deletions common/formatting/basic_format_style_init.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2017-2021 The Verible Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "common/formatting/basic_format_style_init.h"

#include "absl/flags/flag.h"

ABSL_FLAG(int, indentation_spaces, 2,
"Each indentation level adds this many spaces.");

ABSL_FLAG(int, wrap_spaces, 4,
"Each wrap level adds this many spaces. This applies when the first "
"element after an open-group section is wrapped. Otherwise, the "
"indentation level is set to the column position of the open-group "
"operator.");

ABSL_FLAG(int, column_limit, 100,
"Target line length limit to stay under when formatting.");

ABSL_FLAG(int, over_column_limit_penalty, 100,
"For penalty minimization, this represents the baseline penalty "
"value of exceeding the column limit. Additional penalty of 1 is "
"incurred for each character over this limit");

ABSL_FLAG(int, line_break_penalty, 2,
"Penalty added to solution for each introduced line break.");

namespace verible {
void InitializeFromFlags(BasicFormatStyle *style) {
#define STYLE_FROM_FLAG(name) style->name = absl::GetFlag(FLAGS_##name)

// Simply in the sequence as declared in struct BasicFormatStyle
STYLE_FROM_FLAG(indentation_spaces);
STYLE_FROM_FLAG(wrap_spaces);
STYLE_FROM_FLAG(column_limit);
STYLE_FROM_FLAG(over_column_limit_penalty);
STYLE_FROM_FLAG(line_break_penalty);

#undef STYLE_FROM_FLAG
}
} // namespace verible
29 changes: 29 additions & 0 deletions common/formatting/basic_format_style_init.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2017-2021 The Verible Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef VERIBLE_COMMON_FORMATTING_BASIC_FORMAT_STYLE_INIT_H_
#define VERIBLE_COMMON_FORMATTING_BASIC_FORMAT_STYLE_INIT_H_

#include "common/formatting/basic_format_style.h"

namespace verible {

// Initialize format style from flags.
void InitializeFromFlags(BasicFormatStyle *style);

// TODO: initialize from configuration file.
// https://github.com/chipsalliance/verible/issues/898
// Possibly using common/text/config_utils.h
} // namespace verible
#endif // VERIBLE_COMMON_FORMATTING_BASIC_FORMAT_STYLE_INIT_H_
1 change: 1 addition & 0 deletions verilog/formatting/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ cc_library(
hdrs = ["format_style_init.h"],
deps = [
":format_style",
"//common/formatting:basic_format_style_init",
"@com_google_absl//absl/flags:flag",
"@com_google_absl//absl/strings",
],
Expand Down
5 changes: 4 additions & 1 deletion verilog/formatting/format_style_init.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2017-2020 The Verible Authors.
// Copyright 2017-2021 The Verible Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -15,6 +15,7 @@
#include "verilog/formatting/format_style_init.h"

#include "absl/flags/flag.h"
#include "common/formatting/basic_format_style_init.h"

using verible::AlignmentPolicy;
using verible::IndentationStyle;
Expand Down Expand Up @@ -105,6 +106,8 @@ ABSL_RETIRED_FLAG(
namespace verilog {
namespace formatter {
void InitializeFromFlags(FormatStyle *style) {
verible::InitializeFromFlags(style); // Initialize BasicFormatStyle

#define STYLE_FROM_FLAG(name) style->name = absl::GetFlag(FLAGS_##name)

// Simply in the sequence as declared in struct FormatStyle
Expand Down
2 changes: 1 addition & 1 deletion verilog/formatting/format_style_init.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2017-2020 The Verible Authors.
// Copyright 2017-2021 The Verible Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
15 changes: 15 additions & 0 deletions verilog/tools/formatter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,21 @@ get a full set of avilable flags using the `--helpfull` flag.
usage: verible-verilog-format [options] <file> [<file...>]
To pipe from stdin, use '-' as <file>.
Flags from common/formatting/basic_format_style_init.cc:
--column_limit (Target line length limit to stay under when formatting.);
default: 100;
--indentation_spaces (Each indentation level adds this many spaces.);
default: 2;
--line_break_penalty (Penalty added to solution for each introduced line
break.); default: 2;
--over_column_limit_penalty (For penalty minimization, this represents the
baseline penalty value of exceeding the column limit. Additional penalty
of 1 is incurred for each character over this limit); default: 100;
--wrap_spaces (Each wrap level adds this many spaces. This applies when the
first element after an open-group section is wrapped. Otherwise, the
indentation level is set to the column position of the open-group
operator.); default: 4;
Flags from verilog/formatting/format_style_init.cc:
--assignment_statement_alignment (Format various assignments:
{align,flush-left,preserve,infer}); default: infer;
Expand Down

0 comments on commit 858aa94

Please sign in to comment.