예제 #1
0
 List<ANTLRMessage> getMessagesOfType(List<ANTLRMessage> msgs, Class c) {
   List<ANTLRMessage> filtered = new ArrayList<ANTLRMessage>();
   for (ANTLRMessage m : msgs) {
     if (m.getClass() == c) filtered.add(m);
   }
   return filtered;
 }
예제 #2
0
 public void run() {
   Context ctx = new Context();
   Options options = Options.instance(ctx);
   outputKind.init(options);
   multiPolicy.init(options);
   xdiagsSource.init(options);
   xdiagsCompact.init(options);
   caretKind.init(options);
   sourceLineKind.init(options);
   String indentString = "";
   indentString = (summaryIndent == IndentKind.CUSTOM) ? "3" : "0";
   indentString += (detailsIndent == IndentKind.CUSTOM) ? "|3" : "|0";
   indentString += (sourceIndent == IndentKind.CUSTOM) ? "|3" : "|0";
   indentString += (subdiagsIndent == IndentKind.CUSTOM) ? "|3" : "|0";
   options.put("diagsIndentation", indentString);
   MyLog log = new MyLog(ctx);
   JavacMessages messages = JavacMessages.instance(ctx);
   messages.add("tester");
   JCDiagnostic.Factory diags = JCDiagnostic.Factory.instance(ctx);
   log.useSource(new MyFileObject("This is a source line"));
   JCDiagnostic d =
       diags.error(null, log.currentSource(), posKind.pos(), errorKind.key(), "Hello!");
   if (multiKind != MultilineKind.NONE) {
     JCDiagnostic sub = diags.fragment(errorKind.key(), "Hello!");
     if (multiKind.isNested()) sub = new JCDiagnostic.MultilineDiagnostic(sub, List.of(sub));
     List<JCDiagnostic> subdiags = multiKind.isDouble() ? List.of(sub, sub) : List.of(sub);
     d = new JCDiagnostic.MultilineDiagnostic(d, subdiags);
   }
   String diag = log.getDiagnosticFormatter().format(d, messages.getCurrentLocale());
   checkOutput(diag);
 }
예제 #3
0
  void test(String[] opts, String className) throws Exception {
    count++;
    System.err.println("Test " + count + " " + Arrays.asList(opts) + " " + className);
    Path testSrcDir = Paths.get(System.getProperty("test.src"));
    Path testClassesDir = Paths.get(System.getProperty("test.classes"));
    Path classes = Paths.get("classes." + count);
    classes.createDirectory();

    Context ctx = new Context();
    PathFileManager fm = new JavacPathFileManager(ctx, true, null);
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    List<String> options = new ArrayList<String>();
    options.addAll(Arrays.asList(opts));
    options.addAll(Arrays.asList("-verbose", "-XDverboseCompilePolicy", "-d", classes.toString()));
    Iterable<? extends JavaFileObject> compilationUnits =
        fm.getJavaFileObjects(testSrcDir.resolve(className + ".java"));
    StringWriter sw = new StringWriter();
    PrintWriter out = new PrintWriter(sw);
    JavaCompiler.CompilationTask t =
        compiler.getTask(out, fm, null, options, null, compilationUnits);
    boolean ok = t.call();
    System.err.println(sw.toString());
    if (!ok) {
      throw new Exception("compilation failed");
    }

    File expect = new File("classes." + count + "/" + className + ".class");
    if (!expect.exists()) throw new Exception("expected file not found: " + expect);
    long expectedSize = new File(testClassesDir.toString(), className + ".class").length();
    long actualSize = expect.length();
    if (expectedSize != actualSize)
      throw new Exception("wrong size found: " + actualSize + "; expected: " + expectedSize);
  }
예제 #4
0
  public static List<String> loadBeforeAfterText(String filePath) {
    String content;

    try {
      content = FileUtil.loadFile(new File(filePath), true);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }

    List<String> files =
        createTestFiles(
            "",
            content,
            new TestFileFactoryNoModules<String>() {
              @NotNull
              @Override
              public String create(
                  @NotNull String fileName,
                  @NotNull String text,
                  @NotNull Map<String, String> directives) {
                int firstLineEnd = text.indexOf('\n');
                return StringUtil.trimTrailing(text.substring(firstLineEnd + 1));
              }
            });

    Assert.assertTrue("Exactly two files expected: ", files.size() == 2);

    return files;
  }
예제 #5
0
 @NotNull
 public static List<KtFile> loadToJetFiles(
     @NotNull KotlinCoreEnvironment environment, @NotNull List<File> files) throws IOException {
   List<KtFile> jetFiles = Lists.newArrayList();
   for (File file : files) {
     jetFiles.add(loadJetFile(environment.getProject(), file));
   }
   return jetFiles;
 }
예제 #6
0
 public void cleanup() {
   if (!this.flags.contains(Flags.USECACHE)) {
     tempDirs.forEach(
         dir -> {
           deleteDir(dir);
         });
     tempDirs.clear();
   }
 }
예제 #7
0
 private static List<JavaSourceTransformer> getSourceTransformers() {
   final Class<JavaSourceTransformer> transformerClass = JavaSourceTransformer.class;
   final ServiceLoader<JavaSourceTransformer> loader =
       ServiceLoader.load(transformerClass, transformerClass.getClassLoader());
   final List<JavaSourceTransformer> transformers = new ArrayList<JavaSourceTransformer>();
   for (JavaSourceTransformer t : loader) {
     transformers.add(t);
   }
   return transformers;
 }
예제 #8
0
 List<Integer> getTypesFromString(Grammar g, String expecting) {
   List<Integer> expectingTokenTypes = new ArrayList<Integer>();
   if (expecting != null && !expecting.trim().equals("")) {
     for (String tname : expecting.replace(" ", "").split(",")) {
       int ttype = g.getTokenType(tname);
       expectingTokenTypes.add(ttype);
     }
   }
   return expectingTokenTypes;
 }
예제 #9
0
  private static List<String> parseDependencies(@Nullable String dependencies) {
    if (dependencies == null) return Collections.emptyList();

    Matcher matcher = Pattern.compile("\\w+").matcher(dependencies);
    List<String> result = new ArrayList<String>();
    while (matcher.find()) {
      result.add(matcher.group());
    }
    return result;
  }
예제 #10
0
 public List<Integer> getTokenTypes(String input, LexerATNSimulator lexerATN) {
   ANTLRStringStream in = new ANTLRStringStream(input);
   List<Integer> tokenTypes = new ArrayList<Integer>();
   int ttype = 0;
   do {
     ttype = lexerATN.matchATN(in);
     tokenTypes.add(ttype);
   } while (ttype != Token.EOF);
   return tokenTypes;
 }
예제 #11
0
  private File compileOne(Type type) {
    if (this.flags.contains(Flags.USECACHE)) {
      File dir = cache.get(type.getName());
      if (dir != null) {
        return dir;
      }
    }
    List<JavaFileObject> files = new ArrayList<>();
    SourceProcessor accum = (name, src) -> files.add(new SourceFile(name, src));

    for (Type dep : type.typeDependencies()) {
      dep.generateAsDependency(accum, type.methodDependencies());
    }

    type.generate(accum);

    JavacTask ct =
        (JavacTask) this.systemJavaCompiler.getTask(null, this.fm, null, null, null, files);
    File destDir = null;
    do {
      int value = counter.incrementAndGet();
      destDir = new File(root, Integer.toString(value));
    } while (destDir.exists());

    if (this.flags.contains(Flags.VERBOSE)) {
      System.out.println("Compilation unit for " + type.getName() + " : compiled into " + destDir);
      for (JavaFileObject jfo : files) {
        System.out.println(jfo.toString());
      }
    }

    try {
      destDir.mkdirs();
      this.fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(destDir));
    } catch (IOException e) {
      throw new RuntimeException(
          "IOException encountered during compilation: " + e.getMessage(), e);
    }
    Boolean result = ct.call();
    if (result == Boolean.FALSE) {
      throw new RuntimeException("Compilation failure in " + type.getName() + " unit");
    }
    if (this.flags.contains(Flags.USECACHE)) {
      File existing = cache.putIfAbsent(type.getName(), destDir);
      if (existing != null) {
        deleteDir(destDir);
        return existing;
      }
    } else {
      this.tempDirs.add(destDir);
    }
    return destDir;
  }
예제 #12
0
 public List<String> getTokenTypes(LexerGrammar lg, ATN atn, CharStream input, boolean adaptive) {
   LexerATNSimulator interp = new LexerATNSimulator(atn);
   List<String> tokenTypes = new ArrayList<String>();
   int ttype;
   do {
     if (adaptive) ttype = interp.match(input, Lexer.DEFAULT_MODE);
     else ttype = interp.matchATN(input);
     if (ttype == Token.EOF) tokenTypes.add("EOF");
     else {
       tokenTypes.add(lg.typeToTokenList.get(ttype));
     }
   } while (ttype != Token.EOF);
   return tokenTypes;
 }
  private static void loadCommonJavacOptions(CompileContext context) {
    final List<String> options = new ArrayList<String>();
    final List<String> vmOptions = new ArrayList<String>();

    final JpsProject project = context.getProjectDescriptor().getProject();
    final JpsJavaCompilerConfiguration compilerConfig =
        JpsJavaExtensionService.getInstance().getOrCreateCompilerConfiguration(project);
    final JpsJavaCompilerOptions compilerOptions = compilerConfig.getCurrentCompilerOptions();
    if (compilerOptions.DEBUGGING_INFO) {
      options.add("-g");
    }
    if (compilerOptions.DEPRECATION) {
      options.add("-deprecation");
    }
    if (compilerOptions.GENERATE_NO_WARNINGS) {
      options.add("-nowarn");
    }
    if (compilerOptions instanceof EclipseCompilerOptions) {
      final EclipseCompilerOptions eclipseOptions = (EclipseCompilerOptions) compilerOptions;
      if (eclipseOptions.PROCEED_ON_ERROR) {
        options.add("-proceedOnError");
      }
    }
    final String customArgs = compilerOptions.ADDITIONAL_OPTIONS_STRING;
    if (customArgs != null) {
      final StringTokenizer customOptsTokenizer = new StringTokenizer(customArgs, " \t\r\n");
      boolean skip = false;
      while (customOptsTokenizer.hasMoreTokens()) {
        final String userOption = customOptsTokenizer.nextToken();
        if (FILTERED_OPTIONS.contains(userOption)) {
          skip = true;
          continue;
        }
        if (!skip) {
          if (!FILTERED_SINGLE_OPTIONS.contains(userOption)) {
            if (userOption.startsWith("-J-")) {
              vmOptions.add(userOption.substring("-J".length()));
            } else {
              options.add(userOption);
            }
          }
        }
      }
    }

    if (useEclipseCompiler(context)) {
      for (String option : options) {
        if (option.startsWith("-proceedOnError")) {
          Utils.PROCEED_ON_ERROR_KEY.set(context, Boolean.TRUE);
          break;
        }
      }
    }

    JAVAC_OPTIONS.set(context, options);
    JAVAC_VM_OPTIONS.set(context, vmOptions);
  }
예제 #14
0
  @NotNull
  public static <M, F> List<F> createTestFiles(
      String testFileName, String expectedText, TestFileFactory<M, F> factory) {
    Map<String, String> directives = parseDirectives(expectedText);

    List<F> testFiles = Lists.newArrayList();
    Matcher matcher = FILE_OR_MODULE_PATTERN.matcher(expectedText);
    if (!matcher.find()) {
      // One file
      testFiles.add(factory.createFile(null, testFileName, expectedText, directives));
    } else {
      int processedChars = 0;
      M module = null;
      // Many files
      while (true) {
        String moduleName = matcher.group(1);
        String moduleDependencies = matcher.group(2);
        if (moduleName != null) {
          module = factory.createModule(moduleName, parseDependencies(moduleDependencies));
        }

        String fileName = matcher.group(3);
        int start = processedChars;

        boolean nextFileExists = matcher.find();
        int end;
        if (nextFileExists) {
          end = matcher.start();
        } else {
          end = expectedText.length();
        }
        String fileText = expectedText.substring(start, end);
        processedChars = end;

        testFiles.add(factory.createFile(module, fileName, fileText, directives));

        if (!nextFileExists) break;
      }
      assert processedChars == expectedText.length()
          : "Characters skipped from " + processedChars + " to " + (expectedText.length() - 1);
    }
    return testFiles;
  }
예제 #15
0
  public static TestRun compileAndCheck(
      Iterable<? extends JavaFileObject> files, String processor, String[] options) {
    List<String> opts = new LinkedList<String>();
    if (processor != null) {
      opts.add("-processor");
      opts.add(processor);
    }
    opts.add("-source");
    opts.add("1.8");
    for (String option : options) opts.add(option);

    TestInput input =
        new TestInput(files, Collections.<String>emptySet(), opts.toArray(new String[opts.size()]));
    return input.run();
  }
예제 #16
0
 private static Collection<String> prepareOptions(
     final Collection<String> options, boolean usingJavac) {
   final List<String> result = new ArrayList<String>();
   if (usingJavac) {
     result.add("-implicit:class"); // the option supported by javac only
   } else { // is Eclipse
     result.add("-noExit");
   }
   boolean skip = false;
   for (String option : options) {
     if (FILTERED_OPTIONS.contains(option)) {
       skip = true;
       continue;
     }
     if (!skip) {
       if (!FILTERED_SINGLE_OPTIONS.contains(option)) {
         result.add(option);
       }
     }
     skip = false;
   }
   return result;
 }
예제 #17
0
  public static void resolveAllKotlinFiles(KotlinCoreEnvironment environment) throws IOException {
    List<ContentRoot> paths =
        environment.getConfiguration().get(CommonConfigurationKeys.CONTENT_ROOTS);
    if (paths == null) return;
    List<KtFile> jetFiles = Lists.newArrayList();
    for (ContentRoot root : paths) {
      if (!(root instanceof KotlinSourceRoot)) continue;

      String path = ((KotlinSourceRoot) root).getPath();
      File file = new File(path);
      if (file.isFile()) {
        jetFiles.add(loadJetFile(environment.getProject(), file));
      } else {
        //noinspection ConstantConditions
        for (File childFile : file.listFiles()) {
          if (childFile.getName().endsWith(".kt")) {
            jetFiles.add(loadJetFile(environment.getProject(), childFile));
          }
        }
      }
    }
    LazyResolveTestUtil.resolve(environment.getProject(), jetFiles, environment);
  }
예제 #18
0
  @NotNull
  public static List<String> getLastCommentsInFile(
      @NotNull KtFile file, CommentType commentType, boolean assertMustExist) {
    PsiElement lastChild = file.getLastChild();
    if (lastChild != null && lastChild.getNode().getElementType().equals(KtTokens.WHITE_SPACE)) {
      lastChild = lastChild.getPrevSibling();
    }
    assert lastChild != null;

    List<String> comments = ContainerUtil.newArrayList();

    while (true) {
      if (lastChild.getNode().getElementType().equals(KtTokens.BLOCK_COMMENT)) {
        if (commentType == CommentType.ALL || commentType == CommentType.BLOCK_COMMENT) {
          String lastChildText = lastChild.getText();
          comments.add(lastChildText.substring(2, lastChildText.length() - 2).trim());
        }
      } else if (lastChild.getNode().getElementType().equals(KtTokens.EOL_COMMENT)) {
        if (commentType == CommentType.ALL || commentType == CommentType.LINE_COMMENT) {
          comments.add(lastChild.getText().substring(2).trim());
        }
      } else {
        break;
      }

      lastChild = lastChild.getPrevSibling();
    }

    if (comments.isEmpty() && assertMustExist) {
      throw new AssertionError(
          String.format(
              "Test file '%s' should end in a comment of type %s; last node was: %s",
              file.getName(), commentType, lastChild));
    }

    return comments;
  }
예제 #19
0
  public static void deleteOnShutdown(File file) {
    if (filesToDelete.isEmpty()) {
      ShutDownTracker.getInstance()
          .registerShutdownTask(
              new Runnable() {
                @Override
                public void run() {
                  ShutDownTracker.invokeAndWait(
                      true,
                      true,
                      new Runnable() {
                        @Override
                        public void run() {
                          for (File victim : filesToDelete) {
                            FileUtil.delete(victim);
                          }
                        }
                      });
                }
              });
    }

    filesToDelete.add(file);
  }
예제 #20
0
 public static void compileKotlinWithJava(
     @NotNull List<File> javaFiles,
     @NotNull List<File> ktFiles,
     @NotNull File outDir,
     @NotNull Disposable disposable,
     @Nullable File javaErrorFile)
     throws IOException {
   if (!ktFiles.isEmpty()) {
     compileKotlinToDirAndGetAnalysisResult(ktFiles, outDir, disposable, ALL, false);
   } else {
     boolean mkdirs = outDir.mkdirs();
     assert mkdirs : "Not created: " + outDir;
   }
   if (!javaFiles.isEmpty()) {
     compileJavaFiles(
         javaFiles,
         Arrays.asList(
             "-classpath",
             outDir.getPath() + File.pathSeparator + ForTestCompileRuntime.runtimeJarForTests(),
             "-d",
             outDir.getPath()),
         javaErrorFile);
   }
 }
예제 #21
0
  public static String getLastCommentedLines(@NotNull Document document) {
    List<CharSequence> resultLines = new ArrayList<CharSequence>();
    for (int i = document.getLineCount() - 1; i >= 0; i--) {
      int lineStart = document.getLineStartOffset(i);
      int lineEnd = document.getLineEndOffset(i);
      if (document.getCharsSequence().subSequence(lineStart, lineEnd).toString().trim().isEmpty()) {
        continue;
      }

      if ("//"
          .equals(document.getCharsSequence().subSequence(lineStart, lineStart + 2).toString())) {
        resultLines.add(document.getCharsSequence().subSequence(lineStart + 2, lineEnd));
      } else {
        break;
      }
    }
    Collections.reverse(resultLines);
    StringBuilder result = new StringBuilder();
    for (CharSequence line : resultLines) {
      result.append(line).append("\n");
    }
    result.delete(result.length() - 1, result.length());
    return result.toString();
  }
예제 #22
0
 /** Return true if all is ok, no errors */
 protected boolean antlr(
     String fileName, String grammarFileName, String grammarStr, boolean debug) {
   boolean allIsWell = true;
   mkdir(tmpdir);
   writeFile(tmpdir, fileName, grammarStr);
   try {
     final List options = new ArrayList();
     if (debug) {
       options.add("-debug");
     }
     options.add("-o");
     options.add(tmpdir);
     options.add("-lib");
     options.add(tmpdir);
     options.add(new File(tmpdir, grammarFileName).toString());
     final String[] optionsA = new String[options.size()];
     options.toArray(optionsA);
     ErrorQueue equeue = new ErrorQueue();
     Tool antlr = newTool(optionsA);
     antlr.addListener(equeue);
     antlr.processGrammarsOnCommandLine();
     if (equeue.errors.size() > 0) {
       allIsWell = false;
       System.err.println("antlr reports errors from " + options);
       for (int i = 0; i < equeue.errors.size(); i++) {
         ANTLRMessage msg = (ANTLRMessage) equeue.errors.get(i);
         System.err.println(msg);
       }
       System.out.println("!!!\ngrammar:");
       System.out.println(grammarStr);
       System.out.println("###");
     }
   } catch (Exception e) {
     allIsWell = false;
     System.err.println("problems building grammar: " + e);
     e.printStackTrace(System.err);
   }
   return allIsWell;
 }
예제 #23
0
 public Token get(int i) {
   return new org.antlr.v4.runtime.CommonToken(types.get(i));
 }
예제 #24
0
 public Token LT(int i) {
   if ((p + i - 1) >= types.size()) return new CommonToken(-1);
   return new CommonToken(types.get(p + i - 1));
 }
예제 #25
0
 public int size() {
   return types.size();
 }
 public static void registerClassPostProcessor(ClassPostProcessor processor) {
   ourClassProcessors.add(processor);
 }
  public static void addCompilationOptions(
      List<String> options,
      CompileContext context,
      ModuleChunk chunk,
      @Nullable ProcessorConfigProfile profile) {
    if (!isEncodingSet(options)) {
      final CompilerEncodingConfiguration config =
          context.getProjectDescriptor().getEncodingConfiguration();
      final String encoding = config.getPreferredModuleChunkEncoding(chunk);
      if (config.getAllModuleChunkEncodings(chunk).size() > 1) {
        final StringBuilder msgBuilder = new StringBuilder();
        msgBuilder.append("Multiple encodings set for module chunk ").append(chunk.getName());
        if (encoding != null) {
          msgBuilder.append("\n\"").append(encoding).append("\" will be used by compiler");
        }
        context.processMessage(
            new CompilerMessage(BUILDER_NAME, BuildMessage.Kind.INFO, msgBuilder.toString()));
      }
      if (!StringUtil.isEmpty(encoding)) {
        options.add("-encoding");
        options.add(encoding);
      }
    }

    final String langLevel = getLanguageLevel(chunk.getModules().iterator().next());
    if (!StringUtil.isEmpty(langLevel)) {
      options.add("-source");
      options.add(langLevel);
    }

    JpsJavaCompilerConfiguration compilerConfiguration =
        JpsJavaExtensionService.getInstance()
            .getOrCreateCompilerConfiguration(context.getProjectDescriptor().getProject());
    String bytecodeTarget = null;
    int chunkSdkVersion = -1;
    for (JpsModule module : chunk.getModules()) {
      final JpsSdk<JpsDummyElement> sdk = module.getSdk(JpsJavaSdkType.INSTANCE);
      if (sdk != null) {
        final int moduleSdkVersion = convertToNumber(sdk.getVersionString());
        if (moduleSdkVersion != 0 /*could determine the version*/
            && (chunkSdkVersion < 0 || chunkSdkVersion > moduleSdkVersion)) {
          chunkSdkVersion = moduleSdkVersion;
        }
      }

      final String moduleTarget = compilerConfiguration.getByteCodeTargetLevel(module.getName());
      if (moduleTarget == null) {
        continue;
      }
      if (bytecodeTarget == null) {
        bytecodeTarget = moduleTarget;
      } else {
        if (moduleTarget.compareTo(bytecodeTarget) < 0) {
          bytecodeTarget =
              moduleTarget; // use the lower possible target among modules that form the chunk
        }
      }
    }
    if (bytecodeTarget != null) {
      options.add("-target");
      options.add(bytecodeTarget);
    } else {
      if (chunkSdkVersion > 0 && getCompilerSdkVersion(context) > chunkSdkVersion) {
        // force lower bytecode target level to match the version of sdk assigned to this chunk
        options.add("-target");
        options.add("1." + chunkSdkVersion);
      }
    }

    if (profile != null && profile.isEnabled()) {
      // configuring annotation processing
      if (!profile.isObtainProcessorsFromClasspath()) {
        final String processorsPath = profile.getProcessorPath();
        options.add("-processorpath");
        options.add(
            processorsPath == null ? "" : FileUtil.toSystemDependentName(processorsPath.trim()));
      }

      final Set<String> processors = profile.getProcessors();
      if (!processors.isEmpty()) {
        options.add("-processor");
        options.add(StringUtil.join(processors, ","));
      }

      for (Map.Entry<String, String> optionEntry : profile.getProcessorOptions().entrySet()) {
        options.add("-A" + optionEntry.getKey() + "=" + optionEntry.getValue());
      }

      final File srcOutput =
          ProjectPaths.getAnnotationProcessorGeneratedSourcesOutputDir(
              chunk.getModules().iterator().next(), chunk.containsTests(), profile);
      if (srcOutput != null) {
        srcOutput.mkdirs();
        options.add("-s");
        options.add(srcOutput.getPath());
      }
    } else {
      options.add("-proc:none");
    }
  }