summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Martin Stjernholm <mast@google.com> 2022-11-09 23:32:03 +0000
committer Martin Stjernholm <mast@google.com> 2022-11-23 15:28:25 +0000
commit485e6881e537b50c9a751f2405a8c9028b56d3a6 (patch)
treedaba7cd3e7a6e6b07a7cb2657f64ed885bcd17c0
parent44ba423161241ee1b49c6e29e272f8c69f776231 (diff)
Add clang-format preupload check.
This will check that new and changed lines conform to the style defined in .clang-format. Specific code sections can be excluded if it gets in the way (some examples included). Also fix the java style to not line break in "/// CHECK" comments, and use the AOSP style import order. Java files in art/tools/ahat and art/tools/dexfuzz use indentation width 2, so they get an override .clang-format with that style. Java files in test/ have a mix of different indentation widths, so that directory is excluded for the time being. openjdkjvmti/include/jvmti.h is an imported file that should be ignored altogether. However, it is not straightforward to configure a whole file exclude. Upload it with --no-verify if it needs to be updated. Test: clang-format -i art/dex2oat/dex2oat_options.cc \ art/benchmark/const-class/src/ConstClassBenchmark.java \ art/tools/ahat/src/main/com/android/ahat/heapdump/Parser.java \ art/tools/dexfuzz/src/dexfuzz/program/IdCreator.java Check that reformatted files look reasonable Test: Create a local commit with changes in both art/libartservice and art/test, run tools/repohooks/pre-upload.py, and verify that only the changes in art/libartservice get checked. Bug: 181877164 Change-Id: I991ff032694bf5063e8516fdf71a95d0b047ffea
-rw-r--r--.clang-format15
-rw-r--r--.clang-format-java-255
-rw-r--r--PREUPLOAD.cfg42
-rw-r--r--cmdline/cmdline_parser_test.cc2
-rw-r--r--compiler/driver/compiler_options_map-inl.h2
-rw-r--r--dex2oat/dex2oat_options.cc14
-rw-r--r--runtime/parsed_options.cc51
-rw-r--r--test/003-omnibus-opcodes/src/Goto.java4
-rw-r--r--test/083-compiler-regressions/src/Main.java2
-rw-r--r--test/088-monitor-verification/src/Main.java4
-rw-r--r--test/105-invoke/src/Main.java2
-rw-r--r--test/107-int-math2/src/Main.java2
-rw-r--r--test/159-app-image-fields/src/Main.java1
-rw-r--r--test/178-app-image-native-method/src/Main.java2
-rw-r--r--test/710-varhandle-creation/src/Main.java5
l---------tools/ahat/.clang-format1
l---------tools/dexfuzz/.clang-format1
17 files changed, 190 insertions, 15 deletions
diff --git a/.clang-format b/.clang-format
index c2114f78fd..969b941bfa 100644
--- a/.clang-format
+++ b/.clang-format
@@ -1,3 +1,4 @@
+# Please keep in sync with .clang-format-java-2 where applicable.
---
BasedOnStyle: Google
---
@@ -32,9 +33,21 @@ AlignOperands: false
AllowShortFunctionsOnASingleLine: Inline
AlwaysBreakBeforeMultilineStrings: false
ColumnLimit: 100
-CommentPragmas: NOLINT:.*
+CommentPragmas: (NOLINT|CHECK[^ ]*):.*
ConstructorInitializerIndentWidth: 6
ContinuationIndentWidth: 8
IndentWidth: 4
+JavaImportGroups:
+- android
+- androidx
+- com.android
+- dalvik
+- libcore
+- com
+- junit
+- net
+- org
+- java
+- javax
PenaltyBreakBeforeFirstCallParameter: 100000
SpacesBeforeTrailingComments: 1
diff --git a/.clang-format-java-2 b/.clang-format-java-2
new file mode 100644
index 0000000000..3ae2e000fd
--- /dev/null
+++ b/.clang-format-java-2
@@ -0,0 +1,55 @@
+# Variant of .clang-format but with 2 space indent for Java files, for use in
+# subdirectories with that style.
+# Please keep in sync with .clang-format where applicable.
+---
+BasedOnStyle: Google
+---
+
+Language: Cpp
+
+AlignConsecutiveMacros: AcrossComments
+AllowShortBlocksOnASingleLine: Empty
+AllowShortCaseLabelsOnASingleLine: false
+AllowShortIfStatementsOnASingleLine: Never
+AllowShortLoopsOnASingleLine: false
+AttributeMacros: ['__', 'NO_RETURN']
+BinPackArguments: false
+BinPackParameters: false
+BreakConstructorInitializers: BeforeColon
+BreakBeforeTernaryOperators: false
+ColumnLimit: 100
+CommentPragmas: NOLINT:.*
+ConstructorInitializerAllOnOneLineOrOnePerLine: true
+Cpp11BracedListStyle: true
+DerivePointerAlignment: false
+FixNamespaceComments: true
+PointerAlignment: Left
+TabWidth: 2
+
+---
+
+Language: Java
+
+AccessModifierOffset: -2
+AlignOperands: false
+AllowShortFunctionsOnASingleLine: Inline
+AlwaysBreakBeforeMultilineStrings: false
+ColumnLimit: 100
+CommentPragmas: (NOLINT|CHECK[^ ]*):.*
+ConstructorInitializerIndentWidth: 4
+ContinuationIndentWidth: 4
+IndentWidth: 2
+JavaImportGroups:
+- android
+- androidx
+- com.android
+- dalvik
+- libcore
+- com
+- junit
+- net
+- org
+- java
+- javax
+PenaltyBreakBeforeFirstCallParameter: 100000
+SpacesBeforeTrailingComments: 1
diff --git a/PREUPLOAD.cfg b/PREUPLOAD.cfg
index 2182991cab..c5b3c9c18d 100644
--- a/PREUPLOAD.cfg
+++ b/PREUPLOAD.cfg
@@ -10,10 +10,50 @@ check_libnativebridge_test_field = libnativebridge/tests/preupload_check_test_ta
check_expectation_jsons = tools/check_presubmit_json_expectations.sh ${REPO_ROOT} ${PREUPLOAD_FILES}
[Builtin Hooks]
-cpplint = true
bpfmt = true
+clang_format = true
+cpplint = true
gofmt = true
[Builtin Hooks Options]
+# Enable clang-format in all directories except test/, because there are many
+# test Java files with 2 space indent which won't be handled well if they
+# change. Unfortunately there is no way to exclude a directory for a builtin
+# hook.
+clang_format = --commit ${PREUPLOAD_COMMIT} --style file
+ adbconnection/
+ artd/
+ benchmark/
+ build/
+ cmdline/
+ compiler/
+ dalvikvm/
+ dex2oat/
+ dexdump/
+ dexlayout/
+ dexlist/
+ dexoptanalyzer/
+ disassembler/
+ dt_fd_forward/
+ imgdiag/
+ libartbase/
+ libartpalette/
+ libartservice/
+ libarttools/
+ libdexfile/
+ libelffile/
+ libnativebridge/
+ libnativeloader/
+ libprofile/
+ oatdump/
+ odrefresh/
+ openjdkjvm/
+ openjdkjvmti/
+ perfetto_hprof/
+ profman/
+ runtime/
+ sigchainlib/
+ simulator/
+ tools/
# Cpplint prints nothing unless there were errors.
cpplint = --quiet ${PREUPLOAD_FILES}
diff --git a/cmdline/cmdline_parser_test.cc b/cmdline/cmdline_parser_test.cc
index 38c37ec127..effbee93c7 100644
--- a/cmdline/cmdline_parser_test.cc
+++ b/cmdline/cmdline_parser_test.cc
@@ -528,10 +528,12 @@ TEST_F(CmdlineParserTest, TestVerify) {
TEST_F(CmdlineParserTest, TestIgnoreUnrecognized) {
RuntimeParser::Builder parserBuilder;
+ // clang-format off
parserBuilder
.Define("-help")
.IntoKey(M::Help)
.IgnoreUnrecognized(true);
+ // clang-format on
parser_.reset(new RuntimeParser(parserBuilder.Build()));
diff --git a/compiler/driver/compiler_options_map-inl.h b/compiler/driver/compiler_options_map-inl.h
index 6733a81d8a..79a59625f5 100644
--- a/compiler/driver/compiler_options_map-inl.h
+++ b/compiler/driver/compiler_options_map-inl.h
@@ -118,6 +118,7 @@ inline bool ReadCompilerOptions(Base& map, CompilerOptions* options, std::string
template <typename Map, typename Builder>
inline void AddCompilerOptionsArgumentParserOptions(Builder& b) {
+ // clang-format off
b.
Define("--compiler-filter=_")
.template WithType<CompilerFilter::Filter>()
@@ -256,6 +257,7 @@ inline void AddCompilerOptionsArgumentParserOptions(Builder& b) {
.template WithType<unsigned int>()
.WithHelp("Maximum solid block size for compressed images.")
.IntoKey(Map::MaxImageBlockSize);
+ // clang-format on
}
#pragma GCC diagnostic pop
diff --git a/dex2oat/dex2oat_options.cc b/dex2oat/dex2oat_options.cc
index e9ff28b3e6..cf600087a3 100644
--- a/dex2oat/dex2oat_options.cc
+++ b/dex2oat/dex2oat_options.cc
@@ -55,6 +55,7 @@ using Parser = CmdlineParser<Dex2oatArgumentMap, Dex2oatArgumentMap::Key>;
using Builder = Parser::Builder;
static void AddInputMappings(Builder& builder) {
+ // clang-format off
builder.
Define("--dex-file=_")
.WithType<std::vector<std::string>>().AppendValues()
@@ -102,9 +103,11 @@ static void AddInputMappings(Builder& builder) {
" class path components' paths)\n"
"Default: $ANDROID_ROOT/system/framework/boot.art")
.IntoKey(M::BootImage);
+ // clang-format on
}
static void AddGeneratedArtifactMappings(Builder& builder) {
+ // clang-format off
builder.
Define("--input-vdex-fd=_")
.WithType<int>()
@@ -156,9 +159,11 @@ static void AddGeneratedArtifactMappings(Builder& builder) {
"specified by --oat-fd.\n"
"Eg: --oat-location=/data/dalvik-cache/system@app@Calculator.apk.oat")
.IntoKey(M::OatLocation);
+ // clang-format on
}
static void AddImageMappings(Builder& builder) {
+ // clang-format off
builder.
Define("--image=_")
.WithType<std::string>()
@@ -214,9 +219,11 @@ static void AddImageMappings(Builder& builder) {
.WithHelp("Which format to store the image Defaults to uncompressed. Eg:"
" --image-format=lz4")
.IntoKey(M::ImageFormat);
+ // clang-format on
}
static void AddSwapMappings(Builder& builder) {
+ // clang-format off
builder.
Define("--swap-file=_")
.WithType<std::string>()
@@ -234,9 +241,11 @@ static void AddSwapMappings(Builder& builder) {
.WithType<unsigned int>()
.WithHelp("specifies the minimum number of dex file to allow the use of swap.")
.IntoKey(M::SwapDexCountThreshold);
+ // clang-format on
}
static void AddCompilerMappings(Builder& builder) {
+ // clang-format off
builder.
Define("--run-passes=_")
.WithType<std::string>()
@@ -264,9 +273,11 @@ static void AddCompilerMappings(Builder& builder) {
.WithType<std::vector<int>>().AppendValues()
.WithHelp("Specify files containing list of classes preloaded in the zygote.")
.IntoKey(M::PreloadedClassesFds);
+ // clang-format on
}
static void AddTargetMappings(Builder& builder) {
+ // clang-format off
builder.
Define("--instruction-set=_")
.WithType<InstructionSet>()
@@ -288,6 +299,7 @@ static void AddTargetMappings(Builder& builder) {
"Example: --instruction-set-features=div\n"
"Default: default")
.IntoKey(M::TargetInstructionSetFeatures);
+ // clang-format on
}
Parser CreateDex2oatArgumentParser() {
@@ -300,6 +312,7 @@ Parser CreateDex2oatArgumentParser() {
AddCompilerMappings(*parser_builder);
AddTargetMappings(*parser_builder);
+ // clang-format off
parser_builder->
Define({"--watch-dog", "--no-watch-dog"})
.WithHelp("Enable or disable the watchdog timer.")
@@ -450,6 +463,7 @@ Parser CreateDex2oatArgumentParser() {
.WithHelp("Force PaletteNotify{Start,End}Dex2oatCompilation calls.")
.IntoKey(M::ForcePaletteCompilationHooks)
.Ignore({"--comments=_"});
+ // clang-format on
AddCompilerOptionsArgumentParserOptions<Dex2oatArgumentMap>(*parser_builder);
diff --git a/runtime/parsed_options.cc b/runtime/parsed_options.cc
index 6d4a0be441..8e207b2600 100644
--- a/runtime/parsed_options.cc
+++ b/runtime/parsed_options.cc
@@ -82,6 +82,7 @@ std::unique_ptr<RuntimeParser> ParsedOptions::MakeParser(bool ignore_unrecognize
DCHECK_EQ(hiddenapi_policy_valuemap.size(),
static_cast<size_t>(hiddenapi::EnforcementPolicy::kMax) + 1);
+ // clang-format off
parser_builder->
SetCategory("standard")
.Define({"-classpath _", "-cp _"})
@@ -468,18 +469,44 @@ std::unique_ptr<RuntimeParser> ParsedOptions::MakeParser(bool ignore_unrecognize
.WithType<bool>()
.WithValueMap({{"false", false}, {"true", true}})
.IntoKey(M::PerfettoJavaHeapStackProf);
-
- FlagBase::AddFlagsToCmdlineParser(parser_builder.get());
-
- parser_builder->Ignore({
- "-ea", "-da", "-enableassertions", "-disableassertions", "--runtime-arg", "-esa",
- "-dsa", "-enablesystemassertions", "-disablesystemassertions", "-Xrs", "-Xint:_",
- "-Xdexopt:_", "-Xnoquithandler", "-Xjnigreflimit:_", "-Xgenregmap", "-Xnogenregmap",
- "-Xverifyopt:_", "-Xcheckdexsum", "-Xincludeselectedop", "-Xjitop:_",
- "-Xincludeselectedmethod",
- "-Xjitblocking", "-Xjitmethod:_", "-Xjitclass:_", "-Xjitoffset:_",
- "-Xjitosrthreshold:_", "-Xjitconfig:_", "-Xjitcheckcg", "-Xjitverbose", "-Xjitprofile",
- "-Xjitdisableopt", "-Xjitsuspendpoll", "-XX:mainThreadStackSize=_"})
+ // clang-format on
+
+ FlagBase::AddFlagsToCmdlineParser(parser_builder.get());
+
+ parser_builder
+ ->Ignore({"-ea",
+ "-da",
+ "-enableassertions",
+ "-disableassertions",
+ "--runtime-arg",
+ "-esa",
+ "-dsa",
+ "-enablesystemassertions",
+ "-disablesystemassertions",
+ "-Xrs",
+ "-Xint:_",
+ "-Xdexopt:_",
+ "-Xnoquithandler",
+ "-Xjnigreflimit:_",
+ "-Xgenregmap",
+ "-Xnogenregmap",
+ "-Xverifyopt:_",
+ "-Xcheckdexsum",
+ "-Xincludeselectedop",
+ "-Xjitop:_",
+ "-Xincludeselectedmethod",
+ "-Xjitblocking",
+ "-Xjitmethod:_",
+ "-Xjitclass:_",
+ "-Xjitoffset:_",
+ "-Xjitosrthreshold:_",
+ "-Xjitconfig:_",
+ "-Xjitcheckcg",
+ "-Xjitverbose",
+ "-Xjitprofile",
+ "-Xjitdisableopt",
+ "-Xjitsuspendpoll",
+ "-XX:mainThreadStackSize=_"})
.IgnoreUnrecognized(ignore_unrecognized)
.OrderCategories({"standard", "extended", "Dalvik", "ART"});
diff --git a/test/003-omnibus-opcodes/src/Goto.java b/test/003-omnibus-opcodes/src/Goto.java
index d56ceae9d5..35b4e66e60 100644
--- a/test/003-omnibus-opcodes/src/Goto.java
+++ b/test/003-omnibus-opcodes/src/Goto.java
@@ -44,6 +44,7 @@ class Goto {
if (which) {
i += filler(i);
} else {
+ // clang-format off
i -= filler(i); i -= filler(i); i -= filler(i); i -= filler(i);
i -= filler(i); i -= filler(i); i -= filler(i); i -= filler(i);
i -= filler(i); i -= filler(i); i -= filler(i); i -= filler(i);
@@ -54,6 +55,7 @@ class Goto {
i -= filler(i); i -= filler(i); i -= filler(i); i -= filler(i);
i -= filler(i); i -= filler(i); i -= filler(i); i -= filler(i);
i -= filler(i); i -= filler(i); i -= filler(i); i -= filler(i);
+ // clang-format on
}
return i;
@@ -67,6 +69,7 @@ class Goto {
if (which) {
i += filler(i);
} else {
+ // clang-format off
i -= filler(i); i -= filler(i); i -= filler(i); i -= filler(i);
i -= filler(i); i -= filler(i); i -= filler(i); i -= filler(i);
i -= filler(i); i -= filler(i); i -= filler(i); i -= filler(i);
@@ -2392,6 +2395,7 @@ class Goto {
i -= filler(i); i -= filler(i); i -= filler(i); i -= filler(i);
i -= filler(i); i -= filler(i); i -= filler(i); i -= filler(i);
i -= filler(i); i -= filler(i); i -= filler(i); i -= filler(i);
+ // clang-format on
}
return i;
diff --git a/test/083-compiler-regressions/src/Main.java b/test/083-compiler-regressions/src/Main.java
index 9de3f5a5a3..53641b441e 100644
--- a/test/083-compiler-regressions/src/Main.java
+++ b/test/083-compiler-regressions/src/Main.java
@@ -9756,6 +9756,7 @@ class MirOpSelectTests {
private static int ifLez(int src, int thn, int els) { return (src <= 0) ? thn : els; }
public static void testIfCcz() {
+ // clang-format off
int[] results = new int[] {
ifEqzThen0Else1(-1), 1,
ifEqzThen0Else1(0), 0,
@@ -9826,6 +9827,7 @@ class MirOpSelectTests {
ifGtzThen8Else9(0), 9,
ifGtzThen8Else9(1), 8
};
+ // clang-format on
boolean success = true;
StringBuilder fails = new StringBuilder();
diff --git a/test/088-monitor-verification/src/Main.java b/test/088-monitor-verification/src/Main.java
index 3924fa6b8c..3ed1939a08 100644
--- a/test/088-monitor-verification/src/Main.java
+++ b/test/088-monitor-verification/src/Main.java
@@ -119,6 +119,7 @@ public class Main {
* Confirms that we can have 32 nested monitors on one method.
*/
void notExcessiveNesting() {
+ // clang-format off
assertIsManaged();
synchronized (this) { // 1
synchronized (this) { // 2
@@ -153,6 +154,7 @@ public class Main {
synchronized (this) { // 31
synchronized (this) { // 32
}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
+ // clang-format on
}
/**
@@ -160,6 +162,7 @@ public class Main {
* method.
*/
void notNested() {
+ // clang-format off
assertIsManaged();
synchronized (this) {} // 1
synchronized (this) {} // 2
@@ -195,6 +198,7 @@ public class Main {
synchronized (this) {} // 32
synchronized (this) {} // 33
synchronized (this) {} // 34
+ // clang-format on
}
/* does nothing but ensure that the compiler doesn't discard an object */
diff --git a/test/105-invoke/src/Main.java b/test/105-invoke/src/Main.java
index d5f3b62947..d4e4c4e919 100644
--- a/test/105-invoke/src/Main.java
+++ b/test/105-invoke/src/Main.java
@@ -71,6 +71,7 @@ class Main implements InvokeInterface {
static int invoke(int a) {
Main foo = new Main();
+ // clang-format off
return foo.virI_I(a) +
foo.virI_II(a, 1) +
foo.virI_III(a, 1, 2) +
@@ -84,6 +85,7 @@ class Main implements InvokeInterface {
statI_IIIII(a, 1, 2, 3, 4) +
statI_IIIIII(a, 1, 2, 3, 4, 5) +
foo.interfaceMethod(a);
+ // clang-format on
}
public static void main(String[] args) {
diff --git a/test/107-int-math2/src/Main.java b/test/107-int-math2/src/Main.java
index ec5678d5b0..727d47c4bd 100644
--- a/test/107-int-math2/src/Main.java
+++ b/test/107-int-math2/src/Main.java
@@ -14,6 +14,8 @@
* limitations under the License.
*/
+// clang-format off
+
class Main extends IntMathBase {
public static boolean mBoolean1, mBoolean2;
diff --git a/test/159-app-image-fields/src/Main.java b/test/159-app-image-fields/src/Main.java
index e7db1e1f16..d238f773ca 100644
--- a/test/159-app-image-fields/src/Main.java
+++ b/test/159-app-image-fields/src/Main.java
@@ -115,6 +115,7 @@ public class Main {
// and the verification of Main is last and fills the DexCache with Derived.value.
//
class Fields {
+ // clang-format off
public static int clobberDexCache() {
return 0
+ testField0000
diff --git a/test/178-app-image-native-method/src/Main.java b/test/178-app-image-native-method/src/Main.java
index 294ad4739b..877efa4be4 100644
--- a/test/178-app-image-native-method/src/Main.java
+++ b/test/178-app-image-native-method/src/Main.java
@@ -17,6 +17,8 @@
import dalvik.annotation.optimization.FastNative;
import dalvik.annotation.optimization.CriticalNative;
+// clang-format off
+
public class Main {
public static void main(String[] args) throws Exception {
diff --git a/test/710-varhandle-creation/src/Main.java b/test/710-varhandle-creation/src/Main.java
index c4fe2ffed1..6ddc58a91d 100644
--- a/test/710-varhandle-creation/src/Main.java
+++ b/test/710-varhandle-creation/src/Main.java
@@ -271,6 +271,7 @@ public final class Main {
private static void checkInstantiatedVarHandles() {
System.out.print("checkInstantiatedVarHandles...");
+ // clang-format off
System.out.print("vz...");
checkNotNull(vz);
checkVarType(vz, boolean.class);
@@ -2052,6 +2053,7 @@ public final class Main {
System.out.print("vbbo...");
checkNull(vbbo);
+ // clang-format on
System.out.println("PASS");
}
@@ -2090,6 +2092,7 @@ public final class Main {
if (VarHandle.AccessMode.values().length != expectedLength) {
fail("VarHandle.AccessMode.value().length != " + expectedLength);
}
+ // clang-format off
checkAccessMode(VarHandle.AccessMode.GET, "GET", "get", 0);
checkAccessMode(VarHandle.AccessMode.SET, "SET", "set", 1);
checkAccessMode(VarHandle.AccessMode.GET_VOLATILE, "GET_VOLATILE", "getVolatile", 2);
@@ -2121,6 +2124,7 @@ public final class Main {
checkAccessMode(VarHandle.AccessMode.GET_AND_BITWISE_XOR, "GET_AND_BITWISE_XOR", "getAndBitwiseXor", 28);
checkAccessMode(VarHandle.AccessMode.GET_AND_BITWISE_XOR_RELEASE, "GET_AND_BITWISE_XOR_RELEASE", "getAndBitwiseXorRelease", 29);
checkAccessMode(VarHandle.AccessMode.GET_AND_BITWISE_XOR_ACQUIRE, "GET_AND_BITWISE_XOR_ACQUIRE", "getAndBitwiseXorAcquire", 30);
+ // clang-format on
System.out.println("PASS");
}
@@ -2456,4 +2460,3 @@ public final class Main {
checkStaticFieldVarHandleGc();
}
}
-
diff --git a/tools/ahat/.clang-format b/tools/ahat/.clang-format
new file mode 120000
index 0000000000..88ab38e627
--- /dev/null
+++ b/tools/ahat/.clang-format
@@ -0,0 +1 @@
+../../.clang-format-java-2 \ No newline at end of file
diff --git a/tools/dexfuzz/.clang-format b/tools/dexfuzz/.clang-format
new file mode 120000
index 0000000000..88ab38e627
--- /dev/null
+++ b/tools/dexfuzz/.clang-format
@@ -0,0 +1 @@
+../../.clang-format-java-2 \ No newline at end of file