use of com.sun.tools.javac.code.Source in project ceylon-compiler by ceylon.
the class Main method processArgs.
/**
* Process command line arguments: store all command line options
* in `options' table and return all source filenames.
* @param args The array of command line arguments.
*/
protected java.util.List<String> processArgs(String[] flags) {
int ac = 0;
while (ac < flags.length) {
String flag = flags[ac];
ac++;
int j;
for (j = 0; j < recognizedOptions.length; j++) if (recognizedOptions[j].matches(flag))
break;
if (j == recognizedOptions.length) {
error("err.invalid.flag", flag);
return null;
}
Option option = recognizedOptions[j];
if (option.hasArg()) {
if (ac == flags.length) {
error("err.req.arg", flag);
return null;
}
String operand = flags[ac];
ac++;
if (option.process(flag, operand))
return null;
} else {
if (option.process(flag))
return null;
}
}
String sourceString = options.get("-source");
Source source = (sourceString != null) ? Source.lookup(sourceString) : // JDK 5 is the latest supported source version
Source.JDK1_5;
String targetString = options.get("-target");
Target target = (targetString != null) ? Target.lookup(targetString) : // JDK 5 is the latest supported source version
Target.JDK1_5;
// prototype.
if (Character.isDigit(target.name.charAt(0)) && target.compareTo(source.requiredTarget()) < 0) {
if (targetString != null) {
if (sourceString == null) {
warning("warn.target.default.source.conflict", targetString, source.requiredTarget().name);
} else {
warning("warn.source.target.conflict", sourceString, source.requiredTarget().name);
}
return null;
} else {
options.put("-target", source.requiredTarget().name);
}
}
return sourceFileNames;
}
use of com.sun.tools.javac.code.Source in project ceylon-compiler by ceylon.
the class Main method processArgs.
/**
* Process command line arguments: store all command line options in
* `options' table and return all source filenames.
* @param flags The array of command line arguments.
*/
public List<File> processArgs(String[] flags) {
// XXX sb protected
int ac = 0;
while (ac < flags.length) {
String flag = flags[ac];
ac++;
int j;
// quick hack to speed up file processing:
// if the option does not begin with '-', there is no need to check
// most of the compiler options.
int firstOptionToCheck = flag.charAt(0) == '-' ? 0 : recognizedOptions.length - 1;
for (j = firstOptionToCheck; j < recognizedOptions.length; j++) if (recognizedOptions[j].matches(flag))
break;
if (j == recognizedOptions.length) {
error("err.invalid.flag", flag);
return null;
}
Option option = recognizedOptions[j];
if (option.hasArg()) {
if (ac == flags.length) {
error("err.req.arg", flag);
return null;
}
String operand = flags[ac];
ac++;
if (option.process(options, flag, operand))
return null;
} else {
if (option.process(options, flag))
return null;
}
}
if (!checkDirectoryOrURL("-d"))
return null;
if (!checkDirectory("-s"))
return null;
String sourceString = options.get("-source");
Source source = (sourceString != null) ? Source.lookup(sourceString) : Source.DEFAULT;
String targetString = options.get("-target");
Target target = (targetString != null) ? Target.lookup(targetString) : Target.DEFAULT;
// prototype.
if (Character.isDigit(target.name.charAt(0))) {
if (target.compareTo(source.requiredTarget()) < 0) {
if (targetString != null) {
if (sourceString == null) {
warning("warn.target.default.source.conflict", targetString, source.requiredTarget().name);
} else {
warning("warn.source.target.conflict", sourceString, source.requiredTarget().name);
}
return null;
} else {
options.put("-target", source.requiredTarget().name);
}
} else {
if (targetString == null && !source.allowGenerics()) {
options.put("-target", Target.JDK1_4.name);
}
}
}
return filenames.toList();
}
use of com.sun.tools.javac.code.Source in project ceylon-compiler by ceylon.
the class BaseFileManager method getSource.
protected Source getSource() {
String sourceName = options.get(OptionName.SOURCE);
Source source = null;
if (sourceName != null)
source = Source.lookup(sourceName);
return (source != null ? source : Source.DEFAULT);
}
use of com.sun.tools.javac.code.Source in project checker-framework by typetools.
the class SourceChecker method typeProcess.
/**
* Type-check the code using this checker's visitor.
*
* @see Processor#process(Set, RoundEnvironment)
*/
@Override
public void typeProcess(TypeElement e, TreePath p) {
if (javacErrored) {
reportJavacError(p);
return;
}
// Cannot use BugInCF here because it is outside of the try/catch for BugInCF.
if (e == null) {
messager.printMessage(Kind.ERROR, "Refusing to process empty TypeElement");
return;
}
if (p == null) {
messager.printMessage(Kind.ERROR, "Refusing to process empty TreePath in TypeElement: " + e);
return;
}
if (!warnedAboutGarbageCollection) {
String gcUsageMessage = SystemPlume.gcUsageMessage(.25, 60);
if (gcUsageMessage != null) {
messager.printMessage(Kind.WARNING, gcUsageMessage);
warnedAboutGarbageCollection = true;
}
}
Context context = ((JavacProcessingEnvironment) processingEnv).getContext();
Source source = Source.instance(context);
// Also the enum constant Source.JDK1_8 was renamed at some point...
if (!warnedAboutSourceLevel && source.compareTo(Source.lookup("8")) < 0) {
messager.printMessage(Kind.WARNING, "-source " + source.name + " does not support type annotations");
warnedAboutSourceLevel = true;
}
Log log = Log.instance(context);
if (log.nerrors > this.errsOnLastExit) {
this.errsOnLastExit = log.nerrors;
javacErrored = true;
reportJavacError(p);
return;
}
if (visitor == null) {
// there. Don't also cause a NPE here.
return;
}
if (p.getCompilationUnit() != currentRoot) {
setRoot(p.getCompilationUnit());
if (hasOption("filenames")) {
// TODO: Have a command-line option to turn the timestamps on/off too, because
// they are nondeterministic across runs.
// Add timestamp to indicate how long operations are taking.
// Duplicate messages are suppressed, so this might not appear in front of every "
// is type-checking " message (when a file takes less than a second to type-check).
message(Kind.NOTE, Instant.now().toString());
message(Kind.NOTE, "%s is type-checking %s", (Object) this.getClass().getSimpleName(), currentRoot.getSourceFile().getName());
}
}
// Visit the attributed tree.
try {
visitor.visit(p);
warnUnneededSuppressions();
} catch (UserError ce) {
logUserError(ce);
} catch (TypeSystemError ce) {
logTypeSystemError(ce);
} catch (BugInCF ce) {
logBugInCF(ce);
} catch (Throwable t) {
logBugInCF(wrapThrowableAsBugInCF("SourceChecker.typeProcess", t, p));
} finally {
// Also add possibly deferred diagnostics, which will get published back in
// AbstractTypeProcessor.
this.errsOnLastExit = log.nerrors;
}
}
use of com.sun.tools.javac.code.Source in project ceylon-compiler by ceylon.
the class RecognizedOptions method getAll.
/**
* Get all the recognized options.
* @param helper an {@code OptionHelper} to help when processing options
* @return an array of options
*/
public static Option[] getAll(final OptionHelper helper) {
return new Option[] { new Option(G, "opt.g"), new Option(G_NONE, "opt.g.none") {
@Override
public boolean process(Options options, String option) {
options.put("-g:", "none");
return false;
}
}, new Option(G_CUSTOM, "opt.g.lines.vars.source", Option.ChoiceKind.ANYOF, "lines", "vars", "source"), new XOption(XLINT, "opt.Xlint"), new XOption(XLINT_CUSTOM, "opt.Xlint.suboptlist", Option.ChoiceKind.ANYOF, getXLintChoices()), // -nowarn is retained for command-line backward compatibility
new Option(NOWARN, "opt.nowarn") {
@Override
public boolean process(Options options, String option) {
options.put("-Xlint:none", option);
return false;
}
}, new Option(VERBOSE, "opt.verbose"), new Option(VERBOSE_CUSTOM, "opt.verbose.suboptlist") {
public boolean matches(String s) {
return s.startsWith("-verbose:");
}
public boolean process(Options options, String option) {
String suboptions = option.substring(9);
options.put("-verbose:", suboptions);
// enter all the -verbose suboptions as "-verbose:suboption"
for (StringTokenizer t = new StringTokenizer(suboptions, ","); t.hasMoreTokens(); ) {
String tok = t.nextToken();
// make sure all is an alias for --verbose
if (tok.equals("all"))
options.put(VERBOSE, "true");
String opt = "-verbose:" + tok;
options.put(opt, opt);
}
return false;
}
}, // -deprecation is retained for command-line backward compatibility
new Option(DEPRECATION, "opt.deprecation") {
@Override
public boolean process(Options options, String option) {
options.put("-Xlint:deprecation", option);
return false;
}
}, new Option(CLASSPATH, "opt.arg.path", "opt.classpath"), new Option(CP, "opt.arg.path", "opt.classpath") {
@Override
public boolean process(Options options, String option, String arg) {
return super.process(options, "-classpath", arg);
}
}, new COption(CEYLONCWD, "opt.arg.path", "opt.ceyloncwd"), new COption(CEYLONREPO, "opt.arg.url", "opt.ceylonrepo") {
@Override
public boolean process(Options options, String option, String arg) {
if (options != null)
options.addMulti(CEYLONREPO, arg);
return false;
}
}, new COption(CEYLONSYSTEMREPO, "opt.arg.url", "opt.ceylonsystemrepo"), new COption(CEYLONCACHEREPO, "opt.arg.url", "opt.ceyloncacherepo"), new COption(CEYLONNODEFREPOS, "opt.ceylonnodefrepos"), new COption(CEYLONUSER, "opt.arg.value", "opt.ceylonuser"), new COption(CEYLONPASS, "opt.arg.value", "opt.ceylonpass"), new COption(CEYLONNOOSGI, "opt.ceylonnoosgi"), new COption(CEYLONOSGIPROVIDEDBUNDLES, "opt.arg.value", "opt.ceylonosgiprovidedbundles"), new COption(CEYLONNOPOM, "opt.ceylonnopom"), new COption(CEYLONPACK200, "opt.ceylonpack200"), new COption(CEYLONRESOURCEROOT, "opt.arg.path", "opt.ceylonresourceroot"), new COption(CEYLONDISABLEOPT, "opt.ceylondisableopt"), new COption(CEYLONDISABLEOPT_CUSTOM, "opt.ceylondisableopt.suboptlist"), new COption(CEYLONSUPPRESSWARNINGS, "opt.arg.value", "opt.ceylonsuppresswarnings"), new Option(SOURCEPATH, "opt.arg.path", "opt.sourcepath") {
@Override
public boolean process(Options options, String option, String arg) {
if (options != null)
options.addMulti(SOURCEPATH, arg);
return false;
}
}, new COption(CEYLONSOURCEPATH, "opt.arg.directory", "opt.ceylonsourcepath") {
@Override
public boolean process(Options options, String option, String arg) {
if (options != null)
options.addMulti(SOURCEPATH, arg);
return false;
}
}, new COption(CEYLONRESOURCEPATH, "opt.arg.url", "opt.ceylonresourcepath") {
@Override
public boolean process(Options options, String option, String arg) {
if (options != null)
options.addMulti(CEYLONRESOURCEPATH, arg);
return false;
}
}, new Option(BOOTCLASSPATH, "opt.arg.path", "opt.bootclasspath") {
@Override
public boolean process(Options options, String option, String arg) {
options.remove("-Xbootclasspath/p:");
options.remove("-Xbootclasspath/a:");
return super.process(options, option, arg);
}
}, new XOption(XBOOTCLASSPATH_PREPEND, "opt.arg.path", "opt.Xbootclasspath.p"), new XOption(XBOOTCLASSPATH_APPEND, "opt.arg.path", "opt.Xbootclasspath.a"), new XOption(XBOOTCLASSPATH, "opt.arg.path", "opt.bootclasspath") {
@Override
public boolean process(Options options, String option, String arg) {
options.remove("-Xbootclasspath/p:");
options.remove("-Xbootclasspath/a:");
return super.process(options, "-bootclasspath", arg);
}
}, new Option(EXTDIRS, "opt.arg.dirs", "opt.extdirs"), new XOption(DJAVA_EXT_DIRS, "opt.arg.dirs", "opt.extdirs") {
@Override
public boolean process(Options options, String option, String arg) {
return super.process(options, "-extdirs", arg);
}
}, new Option(ENDORSEDDIRS, "opt.arg.dirs", "opt.endorseddirs"), new XOption(DJAVA_ENDORSED_DIRS, "opt.arg.dirs", "opt.endorseddirs") {
@Override
public boolean process(Options options, String option, String arg) {
return super.process(options, "-endorseddirs", arg);
}
}, new Option(PROC, "opt.proc.none.only", Option.ChoiceKind.ONEOF, "none", "only"), new Option(PROCESSOR, "opt.arg.class.list", "opt.processor"), new Option(PROCESSORPATH, "opt.arg.path", "opt.processorpath"), new Option(D, "opt.arg.directory", "opt.d"), new COption(CEYLONOUT, "opt.arg.url", "opt.ceylonout") {
@Override
public boolean process(Options options, String option, String arg) {
return super.process(options, "-d", arg);
}
}, new COption(CEYLONOFFLINE, "opt.ceylonoffline"), new COption(CEYLONTIMEOUT, "opt.arg.number", "opt.ceylontimeout"), new COption(CEYLONCONTINUE, "opt.ceyloncontinue"), new COption(CEYLONPROGRESS, "opt.ceylonprogress"), new COption(CEYLONAUTOEXPORTMAVENDEPENDENCIES, "opt.ceylonautoexportmavendependencies"), new COption(CEYLONFLATCLASSPATH, "opt.ceylonflatclasspath"), new COption(CEYLONOVERRIDES, "opt.arg.url", "opt.ceylonoverrides"), // backwards-compat
new COption(CEYLONMAVENOVERRIDES, "opt.arg.url", "opt.ceylonoverrides") {
@Override
public boolean process(Options options, String option, String arg) {
return super.process(options, "-overrides", arg);
}
}, new Option(S, "opt.arg.directory", "opt.sourceDest"), new Option(IMPLICIT, "opt.implicit", Option.ChoiceKind.ONEOF, "none", "class"), new Option(ENCODING, "opt.arg.encoding", "opt.encoding") {
@Override
public boolean process(Options options, String option, String operand) {
try {
Charset.forName(operand);
options.put(option, operand);
return false;
} catch (UnsupportedCharsetException e) {
helper.error("err.unsupported.encoding", operand);
return true;
} catch (IllegalCharsetNameException e) {
helper.error("err.unsupported.encoding", operand);
return true;
}
}
}, new Option(SOURCE, "opt.arg.release", "opt.source") {
@Override
public boolean process(Options options, String option, String operand) {
Source source = Source.lookup(operand);
if (source == null) {
helper.error("err.invalid.source", operand);
return true;
}
return super.process(options, option, operand);
}
}, new Option(TARGET, "opt.arg.release", "opt.target") {
@Override
public boolean process(Options options, String option, String operand) {
Target target = Target.lookup(operand);
if (target == null) {
helper.error("err.invalid.target", operand);
return true;
}
return super.process(options, option, operand);
}
}, new COption(VERSION, "opt.version") {
@Override
public boolean process(Options options, String option) {
helper.printVersion();
return super.process(options, option);
}
}, new HiddenOption(FULLVERSION) {
@Override
public boolean process(Options options, String option) {
helper.printFullVersion();
return super.process(options, option);
}
}, new HiddenOption(DIAGS) {
@Override
public boolean process(Options options, String option) {
Option xd = getOptions(helper, EnumSet.of(XD))[0];
option = option.substring(option.indexOf('=') + 1);
String diagsOption = option.contains("%") ? "-XDdiagsFormat=" : "-XDdiags=";
diagsOption += option;
if (xd.matches(diagsOption))
return xd.process(options, diagsOption);
else
return false;
}
}, new COption(HELP, "opt.help") {
@Override
public boolean process(Options options, String option) {
helper.printHelp();
return super.process(options, option);
}
}, new Option(A, "opt.arg.key.equals.value", "opt.A") {
@Override
String helpSynopsis() {
hasSuffix = true;
return super.helpSynopsis();
}
@Override
public boolean matches(String arg) {
return arg.startsWith("-A");
}
@Override
public boolean hasArg() {
return false;
}
// Mapping for processor options created in
// JavacProcessingEnvironment
@Override
public boolean process(Options options, String option) {
int argLength = option.length();
if (argLength == 2) {
helper.error("err.empty.A.argument");
return true;
}
int sepIndex = option.indexOf('=');
String key = option.substring(2, (sepIndex != -1 ? sepIndex : argLength));
if (!JavacProcessingEnvironment.isValidOptionName(key)) {
helper.error("err.invalid.A.key", option);
return true;
}
return process(options, option, option);
}
}, new Option(X, "opt.X") {
@Override
public boolean process(Options options, String option) {
helper.printXhelp();
return super.process(options, option);
}
}, // It's actually implemented by the launcher.
new Option(J, "opt.arg.flag", "opt.J") {
@Override
String helpSynopsis() {
hasSuffix = true;
return super.helpSynopsis();
}
@Override
public boolean process(Options options, String option) {
throw new AssertionError("the -J flag should be caught by the launcher.");
}
}, // new Option("-moreinfo", "opt.moreinfo") {
new HiddenOption(MOREINFO) {
@Override
public boolean process(Options options, String option) {
Type.moreInfo = true;
return super.process(options, option);
}
}, // treat warnings as errors
new Option(WERROR, "opt.Werror"), new Option(SRC, "opt.arg.src", "opt.src") {
public boolean process(Options options, String option, String arg) {
return super.process(options, "-src", arg);
}
}, // use complex inference from context in the position of a method call argument
new HiddenOption(COMPLEXINFERENCE), // new Option("-prompt", "opt.prompt"),
new HiddenOption(PROMPT), // dump stack on error
new HiddenOption(DOE), // new Option("-s", "opt.s"),
new HiddenOption(PRINTSOURCE), // allow us to compile ceylon.language
new HiddenOption(BOOTSTRAPCEYLON), // display warnings for generic unchecked operations
new HiddenOption(WARNUNCHECKED) {
@Override
public boolean process(Options options, String option) {
options.put("-Xlint:unchecked", option);
return false;
}
}, new XOption(XMAXERRS, "opt.arg.number", "opt.maxerrs"), new XOption(XMAXWARNS, "opt.arg.number", "opt.maxwarns"), new XOption(XSTDOUT, "opt.arg.file", "opt.Xstdout") {
@Override
public boolean process(Options options, String option, String arg) {
try {
helper.setOut(new PrintWriter(new FileWriter(arg), true));
} catch (java.io.IOException e) {
helper.error("err.error.writing.file", arg, e);
return true;
}
return super.process(options, option, arg);
}
}, new XOption(XPRINT, "opt.print"), new XOption(XPRINTROUNDS, "opt.printRounds"), new XOption(XPRINTPROCESSORINFO, "opt.printProcessorInfo"), new XOption(XPREFER, "opt.prefer", Option.ChoiceKind.ONEOF, "source", "newer"), new XOption(XPKGINFO, "opt.pkginfo", Option.ChoiceKind.ONEOF, "always", "legacy", "nonempty"), /* -O is a no-op, accepted for backward compatibility. */
new HiddenOption(O), /* -Xjcov produces tables to support the code coverage tool jcov. */
new HiddenOption(XJCOV), /* This is a back door to the compiler's option table.
* -XDx=y sets the option x to the value y.
* -XDx sets the option x to the value x.
*/
new HiddenOption(XD) {
String s;
@Override
public boolean matches(String s) {
this.s = s;
return s.startsWith(name.optionName);
}
@Override
public boolean process(Options options, String option) {
s = s.substring(name.optionName.length());
int eq = s.indexOf('=');
String key = (eq < 0) ? s : s.substring(0, eq);
String value = (eq < 0) ? s : s.substring(eq + 1);
options.put(key, value);
return false;
}
}, // It's actually implemented by the CommandLine class.
new Option(AT, "opt.arg.file", "opt.AT") {
@Override
String helpSynopsis() {
hasSuffix = true;
return super.helpSynopsis();
}
@Override
public boolean process(Options options, String option) {
throw new AssertionError("the @ flag should be caught by CommandLine.");
}
}, /*
* TODO: With apt, the matches method accepts anything if
* -XclassAsDecls is used; code elsewhere does the lookup to
* see if the class name is both legal and found.
*
* In apt, the process method adds the candidate class file
* name to a separate list.
*/
new HiddenOption(SOURCEFILE) {
String s;
@Override
public boolean matches(String s) {
this.s = s;
return // Java source file
s.endsWith(".java") || // FIXME: Should be a FileManager query
s.endsWith(".ceylon") || // FIX for ceylon because default is not a valid name for Java
"default".equals(s) || // Legal type name for Ceylon
isCeylonName(s) || // Possibly a resource file
(new File(s)).isFile();
}
@Override
public boolean process(Options options, String option) {
File f = new File(s);
if (s.endsWith(".java") || // FIXME: Should be a FileManager query
s.endsWith(".ceylon")) {
// Most likely a source file
if (!f.isFile()) {
// -sourcepath not -src because the COption for
// CEYLONSOURCEPATH puts it in the options map as -sourcepath
List<String> sourcePaths = options.getMulti("-sourcepath");
if (sourcePaths.isEmpty())
sourcePaths = FileUtil.filesToPathList(DefaultToolOptions.getCompilerSourceDirs());
if (checkIfModule(sourcePaths, s)) {
// A Ceylon module name that ends with .ceylon or .java
helper.addClassName(s);
return false;
}
if (f.exists()) {
helper.error("err.file.not.file", f);
return true;
}
}
if (!f.exists()) {
helper.error("err.file.not.found", f);
return true;
}
helper.addFile(f);
}
if (f.isFile()) {
// Most likely a resource file
helper.addFile(f);
} else {
// the default module is always allowed, it doesn't need to have any folder
if (s.equals(Module.DEFAULT_MODULE_NAME)) {
helper.addClassName(s);
return false;
}
// find a corresponding physical module in the source path
List<String> sourcePaths = options.getMulti("-sourcepath");
if (sourcePaths.isEmpty())
sourcePaths = FileUtil.filesToPathList(DefaultToolOptions.getCompilerSourceDirs());
if (checkIfModule(sourcePaths, s)) {
helper.addClassName(s);
return false;
}
String paths = sourcePaths.toString();
helper.error("err.module.not.found", s, paths.substring(1, paths.length() - 1));
return true;
}
return false;
}
private boolean checkIfModule(List<String> paths, String moduleName) {
String moduleDirName = moduleName.replace(".", File.separator);
// walk every path arg
for (String path : paths) {
// split the path
for (String part : path.split("\\" + File.pathSeparator)) {
// try to see if it's a module folder
File moduleFolder = new File(part, moduleDirName);
if (moduleFolder.isDirectory()) {
return true;
}
}
}
return false;
}
} };
}
Aggregations