Skip to content

Commit

Permalink
Merge pull request chipsalliance#1083 from hzeller/format-style-from-…
Browse files Browse the repository at this point in the history
…flag

Move FormatStyle flag initialization into separate library.
  • Loading branch information
hzeller authored Nov 3, 2021
2 parents 6474bab + 858aa94 commit 5dc8b11
Show file tree
Hide file tree
Showing 11 changed files with 353 additions and 107 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_
12 changes: 12 additions & 0 deletions verilog/formatting/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,18 @@ cc_library(
],
)

cc_library(
name = "format_style_init",
srcs = ["format_style_init.cc"],
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",
],
)

cc_library(
name = "token_annotator",
srcs = ["token_annotator.cc"],
Expand Down
11 changes: 11 additions & 0 deletions verilog/formatting/format_style.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ struct FormatStyle : public verible::BasicFormatStyle {

FormatStyle(const FormatStyle&) = default;

/*
* InitializeFromFlags() [format_style_init.h] provides flags that are
* named like these fields and allow configuration on the command line.
* So field foo here can be configured with flag --foo
*/

// TODO(hzeller): some of these are plural, some singular. Come up with
// a consistent scheme.

// Control indentation amount for port declarations.
IndentationStyle port_declarations_indentation = IndentationStyle::kWrap;

Expand Down Expand Up @@ -104,6 +113,8 @@ struct FormatStyle : public verible::BasicFormatStyle {
// Compact binary expressions inside indexing / bit selection operators
bool compact_indexing_and_selections = true;

// -- Note: when adding new fields, add them in format_style_init.cc

// TODO(fangism): introduce the following knobs:
//
// Unless forced by previous line, starting a line with a comma is
Expand Down
139 changes: 139 additions & 0 deletions verilog/formatting/format_style_init.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
// 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 "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;

ABSL_FLAG(bool, try_wrap_long_lines, false,
"If true, let the formatter attempt to optimize line wrapping "
"decisions where wrapping is needed, else leave them unformatted. "
"This is a short-term measure to reduce risk-of-harm.");

ABSL_FLAG(bool, expand_coverpoints, false,
"If true, always expand coverpoints.");

// These flags exist in the short term to disable formatting of some regions.
// Do not expect to be able to use these in the long term, once they find
// a better home in a configuration struct.

// "indent" means 2 spaces, "wrap" means 4 spaces.
ABSL_FLAG(IndentationStyle, port_declarations_indentation,
IndentationStyle::kWrap, "Indent port declarations: {indent,wrap}");
ABSL_FLAG(IndentationStyle, formal_parameters_indentation,
IndentationStyle::kWrap, "Indent formal parameters: {indent,wrap}");
ABSL_FLAG(IndentationStyle, named_parameter_indentation,
IndentationStyle::kWrap,
"Indent named parameter assignments: {indent,wrap}");
ABSL_FLAG(IndentationStyle, named_port_indentation, IndentationStyle::kWrap,
"Indent named port connections: {indent,wrap}");

// For most of the following in this group, kInferUserIntent is a reasonable
// default behavior because it allows for user-control with minimal invasiveness
// and burden on the user.
ABSL_FLAG(AlignmentPolicy, port_declarations_alignment,
AlignmentPolicy::kInferUserIntent,
"Format port declarations: {align,flush-left,preserve,infer}");
ABSL_FLAG(AlignmentPolicy, struct_union_members_alignment,
AlignmentPolicy::kInferUserIntent,
"Format struct/union members: {align,flush-left,preserve,infer}");
ABSL_FLAG(AlignmentPolicy, named_parameter_alignment,
AlignmentPolicy::kInferUserIntent,
"Format named actual parameters: {align,flush-left,preserve,infer}");
ABSL_FLAG(AlignmentPolicy, named_port_alignment,
AlignmentPolicy::kInferUserIntent,
"Format named port connections: {align,flush-left,preserve,infer}");
ABSL_FLAG(
AlignmentPolicy, module_net_variable_alignment, //
AlignmentPolicy::kInferUserIntent,
"Format net/variable declarations: {align,flush-left,preserve,infer}");
ABSL_FLAG(AlignmentPolicy, formal_parameters_alignment,
AlignmentPolicy::kInferUserIntent,
"Format formal parameters: {align,flush-left,preserve,infer}");
ABSL_FLAG(AlignmentPolicy, class_member_variable_alignment,
AlignmentPolicy::kInferUserIntent,
"Format class member variables: {align,flush-left,preserve,infer}");
ABSL_FLAG(AlignmentPolicy, case_items_alignment,
AlignmentPolicy::kInferUserIntent,
"Format case items: {align,flush-left,preserve,infer}");
ABSL_FLAG(AlignmentPolicy, distribution_items_alignment,
AlignmentPolicy::kInferUserIntent,
"Aligh distribution items: {align,flush-left,preserve,infer}");
ABSL_FLAG(AlignmentPolicy, assignment_statement_alignment,
AlignmentPolicy::kInferUserIntent,
"Format various assignments: {align,flush-left,preserve,infer}");
ABSL_FLAG(AlignmentPolicy, enum_assignment_statement_alignment,
AlignmentPolicy::kInferUserIntent,
"Format assignments with enums: {align,flush-left,preserve,infer}");

ABSL_FLAG(bool, compact_indexing_and_selections, true,
"Use compact binary expressions inside indexing / bit selection "
"operators");
ABSL_FLAG(bool, port_declarations_right_align_packed_dimensions, false,
"If true, packed dimensions in contexts with enabled alignment are "
"aligned to the right.");

ABSL_FLAG(bool, port_declarations_right_align_unpacked_dimensions, false,
"If true, unpacked dimensions in contexts with enabled alignment are "
"aligned to the right.");

// -- Deprecated flags. These were typos. Remove after 2022-01-01
ABSL_RETIRED_FLAG(
AlignmentPolicy, net_variable_alignment, //
AlignmentPolicy::kInferUserIntent,
"Format net/variable declarations: {align,flush-left,preserve,infer}");

ABSL_RETIRED_FLAG(
AlignmentPolicy, class_member_variables_alignment,
AlignmentPolicy::kInferUserIntent,
"Format class member variables: {align,flush-left,preserve,infer}");

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
STYLE_FROM_FLAG(port_declarations_indentation);
STYLE_FROM_FLAG(port_declarations_alignment);
STYLE_FROM_FLAG(struct_union_members_alignment);
STYLE_FROM_FLAG(named_parameter_indentation);
STYLE_FROM_FLAG(named_parameter_alignment);
STYLE_FROM_FLAG(named_port_indentation);
STYLE_FROM_FLAG(named_port_alignment);
STYLE_FROM_FLAG(module_net_variable_alignment);
STYLE_FROM_FLAG(assignment_statement_alignment);
STYLE_FROM_FLAG(enum_assignment_statement_alignment);
STYLE_FROM_FLAG(formal_parameters_indentation);
STYLE_FROM_FLAG(formal_parameters_alignment);
STYLE_FROM_FLAG(class_member_variable_alignment);
STYLE_FROM_FLAG(case_items_alignment);
STYLE_FROM_FLAG(distribution_items_alignment);
STYLE_FROM_FLAG(port_declarations_right_align_packed_dimensions);
STYLE_FROM_FLAG(port_declarations_right_align_unpacked_dimensions);
STYLE_FROM_FLAG(try_wrap_long_lines);
STYLE_FROM_FLAG(expand_coverpoints);
STYLE_FROM_FLAG(compact_indexing_and_selections);

#undef STYLE_FROM_FLAG
}

} // namespace formatter
} // namespace verilog
31 changes: 31 additions & 0 deletions verilog/formatting/format_style_init.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// 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_VERILOG_FORMATTING_FORMAT_STYLE_INIT_H_
#define VERIBLE_VERILOG_FORMATTING_FORMAT_STYLE_INIT_H_

#include "verilog/formatting/format_style.h"

namespace verilog {
namespace formatter {

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

// TODO: initialize from configuration file.
// https://github.com/chipsalliance/verible/issues/898
// Possibly using common/text/config_utils.h
} // namespace formatter
} // namespace verilog
#endif // VERIBLE_VERILOG_FORMATTING_FORMAT_STYLE_INIT_H_
1 change: 1 addition & 0 deletions verilog/tools/formatter/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ cc_binary(
"//common/util:interval_set",
"//common/util:logging",
"//verilog/formatting:format_style",
"//verilog/formatting:format_style_init",
"//verilog/formatting:formatter",
"@com_google_absl//absl/flags:flag",
"@com_google_absl//absl/flags:usage",
Expand Down
Loading

0 comments on commit 5dc8b11

Please sign in to comment.