All Downloads are FREE. Search and download functionalities are using the official Maven repository.

au.com.integradev.delphi.executor.DelphiChecksExecutor Maven / Gradle / Ivy

The newest version!
/*
 * Sonar Delphi Plugin
 * Copyright (C) 2019 Integrated Application Development
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
 */
package au.com.integradev.delphi.executor;

import au.com.integradev.delphi.check.DelphiCheckContextImpl;
import au.com.integradev.delphi.check.MasterCheckRegistrar;
import au.com.integradev.delphi.compiler.Platform;
import au.com.integradev.delphi.file.DelphiFile.DelphiInputFile;
import au.com.integradev.delphi.msbuild.DelphiProjectHelper;
import au.com.integradev.delphi.preprocessor.directive.CompilerDirectiveParserImpl;
import java.util.Set;
import java.util.function.Function;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.SonarProduct;
import org.sonar.api.SonarRuntime;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.rule.RuleScope;
import org.sonar.api.utils.AnnotationUtils;
import org.sonar.plugins.communitydelphi.api.check.DelphiCheck;
import org.sonar.plugins.communitydelphi.api.check.DelphiCheckContext;
import org.sonar.plugins.communitydelphi.api.check.SonarLintUnsupported;
import org.sonar.plugins.communitydelphi.api.directive.CompilerDirectiveParser;

public class DelphiChecksExecutor implements Executor {
  private static final Logger LOG = LoggerFactory.getLogger(DelphiChecksExecutor.class);

  private final DelphiProjectHelper delphiProjectHelper;
  private final MasterCheckRegistrar checkRegistrar;
  private final SonarRuntime sonarRuntime;

  public DelphiChecksExecutor(
      DelphiProjectHelper delphiProjectHelper,
      MasterCheckRegistrar checkRegistrar,
      SonarRuntime sonarRuntime) {
    this.delphiProjectHelper = delphiProjectHelper;
    this.checkRegistrar = checkRegistrar;
    this.sonarRuntime = sonarRuntime;
  }

  @Override
  public void execute(Context context, DelphiInputFile delphiFile) {
    Platform platform = delphiProjectHelper.getToolchain().platform;
    CompilerDirectiveParser compilerDirectiveParser =
        new CompilerDirectiveParserImpl(platform, delphiFile.getTextBlockLineEndingModeRegistry());
    Function createCheckContext =
        check ->
            new DelphiCheckContextImpl(
                check,
                context.sensorContext(),
                delphiFile,
                compilerDirectiveParser,
                checkRegistrar);

    runChecks(RuleScope.ALL, createCheckContext);
    // Main files may contain test code.
    runChecks(RuleScope.TEST, createCheckContext);
    // Test files do not contain main code.
    if (delphiFile.getInputFile().type() != InputFile.Type.TEST) {
      runChecks(RuleScope.MAIN, createCheckContext);
    }
  }

  private void runChecks(
      RuleScope scope, Function createCheckContext) {
    checkRegistrar.getChecks(scope).stream()
        .filter(this::isCheckSupportedOnPlatform)
        .forEach(
            check -> {
              DelphiCheckContext context = createCheckContext.apply(check);
              try {
                check.start(context);
                check.visit(context.getAst(), context);
                check.end(context);
              } catch (Exception e) {
                LOG.error(
                    "Error occurred while running check {} on file: {}",
                    check.getClass().getSimpleName(),
                    context.getAst().getDelphiFile().getSourceCodeFile().getName(),
                    e);
                LOG.info("Continuing with next check.");
              }
            });
  }

  private boolean isCheckSupportedOnPlatform(DelphiCheck check) {
    return sonarRuntime.getProduct() == SonarProduct.SONARQUBE
        || AnnotationUtils.getAnnotation(check, SonarLintUnsupported.class) == null;
  }

  @Override
  public Set> dependencies() {
    return Set.of(DelphiSymbolTableExecutor.class, DelphiNoSonarExecutor.class);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy