Skip to content

Commit

Permalink
Parse alwaysinline and identifiernamestring rules
Browse files Browse the repository at this point in the history
  • Loading branch information
fergal-whyte committed Oct 25, 2023
1 parent f5f04cb commit f92fc63
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 0 deletions.
3 changes: 3 additions & 0 deletions base/src/main/java/proguard/ConfigurationConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ public class ConfigurationConstants
public static final String DONT_PROCESS_KOTLIN_METADATA = "-dontprocesskotlinmetadata";
public static final String OPTIMIZE_AGGRESSIVELY = "-optimizeaggressively";

public static final String ALWAYS_INLINE = "-alwaysinline";
public static final String IDENTIFIER_NAME_STRING = "-identifiernamestring";

public static final String ANY_FILE_KEYWORD = "**";

public static final String ANY_ATTRIBUTE_KEYWORD = "*";
Expand Down
17 changes: 17 additions & 0 deletions base/src/main/java/proguard/ConfigurationParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,8 @@ else if (ConfigurationConstants.REPACKAGE_CLASSES_OPTION
else if (ConfigurationConstants.DUMP_OPTION .startsWith(nextWord)) configuration.dump = parseOptionalFile();
else if (ConfigurationConstants.ADD_CONFIGURATION_DEBUGGING_OPTION .startsWith(nextWord)) configuration.addConfigurationDebugging = parseNoArgument(true);
else if (ConfigurationConstants.OPTIMIZE_AGGRESSIVELY .startsWith(nextWord)) configuration.optimizeConservatively = parseNoArgument(false);
else if (ConfigurationConstants.ALWAYS_INLINE .startsWith(nextWord)) parseUnsupportedR8Rules(ConfigurationConstants.ALWAYS_INLINE, true);
else if (ConfigurationConstants.IDENTIFIER_NAME_STRING .startsWith(nextWord)) parseUnsupportedR8Rules(ConfigurationConstants.IDENTIFIER_NAME_STRING, true);
else
{
throw new ParseException("Unknown option " + reader.locationDescription());
Expand Down Expand Up @@ -2027,6 +2029,21 @@ private void checkMethodAccessFlags(int requiredSetMemberAccessFlags,
}


private void parseUnsupportedR8Rules(String option, boolean parseClassSpecification) throws IOException, ParseException
{
readNextWord();

if (parseClassSpecification)
{
parseClassSpecificationArguments();
}

System.out.println("Warning: The R8 option " + option + " is currently not supported by ProGuard.\n" +
"This option will have no effect on the optimized artifact.");

}


/**
* A main method for testing configuration parsing.
*/
Expand Down
83 changes: 83 additions & 0 deletions base/src/test/kotlin/proguard/ConfigurationParserTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ import io.kotest.assertions.throwables.shouldThrow
import io.kotest.core.spec.style.FreeSpec
import io.kotest.matchers.shouldBe
import io.kotest.matchers.shouldNotBe
import io.kotest.matchers.string.shouldContain
import proguard.classfile.AccessConstants.PUBLIC
import testutils.asConfiguration
import java.io.ByteArrayOutputStream
import java.io.PrintStream

/**
* Some simple testcases to catch special cases when parsing the Configuration.
Expand Down Expand Up @@ -93,6 +96,86 @@ class ConfigurationParserTest : FreeSpec({
}
}

"Testing -alwaysinline parsing" - {
"Given an empty configuration" - {
val savedPrintStream = System.out
val customOutputStream = ByteArrayOutputStream()
System.setOut(PrintStream(customOutputStream))

parseConfiguration("")

"The option does not print anything" {
customOutputStream.toString() shouldContain ""
System.setOut(savedPrintStream)
}
}

"Given a configuration with -alwaysinline" - {
val savedPrintStream = System.out
val customOutputStream = ByteArrayOutputStream()
System.setOut(PrintStream(customOutputStream))

parseConfiguration(
"""-alwaysinline class * {
@org.chromium.build.annotations.AlwaysInline *;
}
"""
)

"The option prints out a warning" {
customOutputStream.toString() shouldContain "Warning: The R8 option -alwaysinline is currently not supported by ProGuard.\n" +
"This option will have no effect on the optimized artifact."
System.setOut(savedPrintStream)
}
}

"Given a configuration with -alwaysinline with no class specification" - {
"The parsing should throw an exception" {
shouldThrow<ParseException> { parseConfiguration("-alwaysinline") }
}
}
}

"Testing -identifiernamestring parsing" - {
"Given an empty configuration" - {
val savedPrintStream = System.out
val customOutputStream = ByteArrayOutputStream()
System.setOut(PrintStream(customOutputStream))

parseConfiguration("")

"The option does not print anything" {
customOutputStream.toString() shouldContain ""
System.setOut(savedPrintStream)
}
}

"Given a configuration with -identifiernamestring" - {
val savedPrintStream = System.out
val customOutputStream = ByteArrayOutputStream()
System.setOut(PrintStream(customOutputStream))

parseConfiguration(
"""-identifiernamestring class * {
@org.chromium.build.annotations.IdentifierNameString *;
}
"""
)

"The option prints out a warning" {
customOutputStream.toString() shouldContain "Warning: The R8 option -identifiernamestring is currently not supported by ProGuard.\n" +
"This option will have no effect on the optimized artifact."
System.setOut(savedPrintStream)
}
}

"Given a configuration with -identifiernamestring with no class specification" - {
"The parsing should throw an exception" {
shouldThrow<ParseException> { parseConfiguration("-identifiernamestring") }
}
}
}

"Wildcard type tests" - {
class TestConfig(
val configOption: String,
Expand Down
40 changes: 40 additions & 0 deletions base/src/test/kotlin/proguard/ConfigurationWriterTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,44 @@ class ConfigurationWriterTest : FreeSpec({
}
}
}

"Given a -alwaysinline rule" - {
val rules = "-alwaysinline class ** {*;}"

"When the rule is parsed and printed out again" - {
val out = printConfiguration(rules)

"Then the rule should be not be present in the output" {
out shouldBe ""
}
}

"When the rule does not exist it shouldn't be printed out" - {
val out = printConfiguration("")

"Then the rule should not be present in the output" {
out shouldBe ""
}
}
}

"Given a -identifiernamestring rule" - {
val rules = "-identifiernamestring class ** {*;}"

"When the rule is parsed and printed out again" - {
val out = printConfiguration(rules)

"Then the rule should be not be present in the output" {
out shouldBe ""
}
}

"When the rule does not exist it shouldn't be printed out" - {
val out = printConfiguration("")

"Then the rule should not be present in the output" {
out shouldBe ""
}
}
}
})

0 comments on commit f92fc63

Please sign in to comment.