Skip to content

Commit

Permalink
[flang][driver] Add extension options and -finput-charset
Browse files Browse the repository at this point in the history
Add the following options:
* -fimplicit-none and -fno-implicit-none
* -fbackslash and -fno-backslash
* -flogical-abbreviations and -fno-logical-abbreviations
* -fxor-operator and -fno-xor-operator
* -falternative-parameter-statement
* -finput-charset=<value>

Summary of changes:
- Enable extensions in CompilerInvocation#ParseFrontendArgs
- Add encoding_ to Fortran::frontend::FrontendOptions
- Add encoding to Fortran::parser::Options

Differential Revision: https://reviews.llvm.org/D96407
  • Loading branch information
Faris Rehman committed Feb 16, 2021
1 parent 787d771 commit 10826ea
Show file tree
Hide file tree
Showing 13 changed files with 179 additions and 8 deletions.
21 changes: 18 additions & 3 deletions clang/include/clang/Driver/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -1714,7 +1714,8 @@ def fexperimental_strict_floating_point : Flag<["-"], "fexperimental-strict-floa
Group<f_clang_Group>, Flags<[CC1Option]>,
HelpText<"Enables experimental strict floating point in LLVM.">,
MarshallingInfoFlag<LangOpts<"ExpStrictFP">>;
def finput_charset_EQ : Joined<["-"], "finput-charset=">, Group<f_Group>;
def finput_charset_EQ : Joined<["-"], "finput-charset=">, Flags<[FlangOption, FC1Option]>, Group<f_Group>,
HelpText<"Specify the default character set for source files">;
def fexec_charset_EQ : Joined<["-"], "fexec-charset=">, Group<f_Group>;
def finstrument_functions : Flag<["-"], "finstrument-functions">, Group<f_Group>, Flags<[CC1Option]>,
HelpText<"Generate calls to instrument function entry and exit">,
Expand Down Expand Up @@ -4170,7 +4171,6 @@ defm aggressive_function_elimination : BooleanFFlag<"aggressive-function-elimina
defm align_commons : BooleanFFlag<"align-commons">, Group<gfortran_Group>;
defm all_intrinsics : BooleanFFlag<"all-intrinsics">, Group<gfortran_Group>;
defm automatic : BooleanFFlag<"automatic">, Group<gfortran_Group>;
defm backslash : BooleanFFlag<"backslash">, Group<gfortran_Group>;
defm backtrace : BooleanFFlag<"backtrace">, Group<gfortran_Group>;
defm bounds_check : BooleanFFlag<"bounds-check">, Group<gfortran_Group>;
defm check_array_temporaries : BooleanFFlag<"check-array-temporaries">, Group<gfortran_Group>;
Expand All @@ -4187,7 +4187,6 @@ defm dump_parse_tree : BooleanFFlag<"dump-parse-tree">, Group<gfortran_Group>;
defm external_blas : BooleanFFlag<"external-blas">, Group<gfortran_Group>;
defm f2c : BooleanFFlag<"f2c">, Group<gfortran_Group>;
defm frontend_optimize : BooleanFFlag<"frontend-optimize">, Group<gfortran_Group>;
defm implicit_none : BooleanFFlag<"implicit-none">, Group<gfortran_Group>;
defm init_local_zero : BooleanFFlag<"init-local-zero">, Group<gfortran_Group>;
defm integer_4_integer_8 : BooleanFFlag<"integer-4-integer-8">, Group<gfortran_Group>;
defm intrinsic_modules_path : BooleanFFlag<"intrinsic-modules-path">, Group<gfortran_Group>;
Expand Down Expand Up @@ -4241,6 +4240,22 @@ file}]>;
def ffixed_line_length_VALUE : Joined<["-"], "ffixed-line-length-">, Group<f_Group>, Alias<ffixed_line_length_EQ>;
def fopenacc : Flag<["-"], "fopenacc">, Group<f_Group>,
HelpText<"Enable OpenACC">;
def fbackslash : Flag<["-"], "fbackslash">, Group<f_Group>,
HelpText<"Specify that backslash in string introduces an escape character">,
DocBrief<[{Change the interpretation of backslashes in string literals from
a single backslash character to "C-style" escape characters.}]>;
def fno_backslash : Flag<["-"], "fno-backslash">, Group<f_Group>;
def fxor_operator : Flag<["-"], "fxor-operator">, Group<f_Group>,
HelpText<"Enable .XOR. as a synonym of .NEQV.">;
def fno_xor_operator : Flag<["-"], "fno-xor-operator">, Group<f_Group>;
def flogical_abbreviations : Flag<["-"], "flogical-abbreviations">, Group<f_Group>,
HelpText<"Enable logical abbreviations">;
def fno_logical_abbreviations : Flag<["-"], "fno-logical-abbreviations">, Group<f_Group>;
def fimplicit_none : Flag<["-"], "fimplicit-none">, Group<f_Group>,
HelpText<"No implicit typing allowed unless overridden by IMPLICIT statements">;
def fno_implicit_none : Flag<["-"], "fno-implicit-none">, Group<f_Group>;
def falternative_parameter_statement : Flag<["-"], "falternative-parameter-statement">, Group<f_Group>,
HelpText<"Enable the old style PARAMETER statement">;

}

Expand Down
13 changes: 10 additions & 3 deletions clang/lib/Driver/ToolChains/Flang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,16 @@ using namespace llvm::opt;

void Flang::AddFortranDialectOptions(const ArgList &Args,
ArgStringList &CmdArgs) const {
Args.AddAllArgs(CmdArgs, {options::OPT_ffixed_form, options::OPT_ffree_form,
options::OPT_ffixed_line_length_EQ,
options::OPT_fopenmp, options::OPT_fopenacc});
Args.AddAllArgs(CmdArgs,
{options::OPT_ffixed_form, options::OPT_ffree_form,
options::OPT_ffixed_line_length_EQ, options::OPT_fopenmp,
options::OPT_fopenacc, options::OPT_finput_charset_EQ,
options::OPT_fimplicit_none, options::OPT_fno_implicit_none,
options::OPT_fbackslash, options::OPT_fno_backslash,
options::OPT_flogical_abbreviations,
options::OPT_fno_logical_abbreviations,
options::OPT_fxor_operator, options::OPT_fno_xor_operator,
options::OPT_falternative_parameter_statement});
}

void Flang::AddPreprocessingOptions(const ArgList &Args,
Expand Down
4 changes: 4 additions & 0 deletions flang/include/flang/Frontend/FrontendOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#define LLVM_FLANG_FRONTEND_FRONTENDOPTIONS_H

#include "flang/Common/Fortran-features.h"
#include "flang/Parser/characters.h"
#include "flang/Parser/unparse.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/MemoryBuffer.h"
Expand Down Expand Up @@ -174,6 +175,9 @@ class FrontendOptions {
// Language features
common::LanguageFeatureControl features_;

// Source file encoding
Fortran::parser::Encoding encoding_{Fortran::parser::Encoding::UTF_8};

public:
FrontendOptions() : showHelp_(false), showVersion_(false) {}

Expand Down
1 change: 1 addition & 0 deletions flang/include/flang/Parser/parsing.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ struct Options {
bool instrumentedParse{false};
bool isModuleFile{false};
bool needProvenanceRangeToCharBlockMappings{false};
Fortran::parser::Encoding encoding{Fortran::parser::Encoding::UTF_8};
};

class Parsing {
Expand Down
2 changes: 2 additions & 0 deletions flang/lib/Frontend/CompilerInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ bool CompilerInstance::ExecuteAction(FrontendAction &act) {
invoc.setDefaultPredefinitions();
// Update the fortran options based on user-based input.
invoc.setFortranOpts();
// Set the encoding to read all input files in based on user input.
allSources_->set_encoding(invoc.fortranOpts().encoding);
// Create the semantics context and set semantic options.
invoc.setSemanticsOpts(*this->allCookedSources_);

Expand Down
44 changes: 44 additions & 0 deletions flang/lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,49 @@ static InputKind ParseFrontendArgs(FrontendOptions &opts,
if (args.hasArg(clang::driver::options::OPT_fopenmp)) {
opts.features_.Enable(Fortran::common::LanguageFeature::OpenMP);
}
if (const llvm::opt::Arg *arg =
args.getLastArg(clang::driver::options::OPT_fimplicit_none,
clang::driver::options::OPT_fno_implicit_none)) {
opts.features_.Enable(
Fortran::common::LanguageFeature::ImplicitNoneTypeAlways,
arg->getOption().matches(clang::driver::options::OPT_fimplicit_none));
}
if (const llvm::opt::Arg *arg =
args.getLastArg(clang::driver::options::OPT_fbackslash,
clang::driver::options::OPT_fno_backslash)) {
opts.features_.Enable(Fortran::common::LanguageFeature::BackslashEscapes,
arg->getOption().matches(clang::driver::options::OPT_fbackslash));
}
if (const llvm::opt::Arg *arg =
args.getLastArg(clang::driver::options::OPT_flogical_abbreviations,
clang::driver::options::OPT_fno_logical_abbreviations)) {
opts.features_.Enable(
Fortran::common::LanguageFeature::LogicalAbbreviations,
arg->getOption().matches(
clang::driver::options::OPT_flogical_abbreviations));
}
if (const llvm::opt::Arg *arg =
args.getLastArg(clang::driver::options::OPT_fxor_operator,
clang::driver::options::OPT_fno_xor_operator)) {
opts.features_.Enable(Fortran::common::LanguageFeature::XOROperator,
arg->getOption().matches(clang::driver::options::OPT_fxor_operator));
}
if (args.hasArg(
clang::driver::options::OPT_falternative_parameter_statement)) {
opts.features_.Enable(Fortran::common::LanguageFeature::OldStyleParameter);
}
if (const llvm::opt::Arg *arg =
args.getLastArg(clang::driver::options::OPT_finput_charset_EQ)) {
llvm::StringRef argValue = arg->getValue();
if (argValue == "utf-8") {
opts.encoding_ = Fortran::parser::Encoding::UTF_8;
} else if (argValue == "latin-1") {
opts.encoding_ = Fortran::parser::Encoding::LATIN_1;
} else {
diags.Report(clang::diag::err_drv_invalid_value)
<< arg->getAsString(args) << argValue;
}
}
return dashX;
}

Expand Down Expand Up @@ -376,6 +419,7 @@ void CompilerInvocation::setFortranOpts() {
fortranOptions.fixedFormColumns = frontendOptions.fixedFormColumns_;

fortranOptions.features = frontendOptions.features_;
fortranOptions.encoding = frontendOptions.encoding_;

collectMacroDefinitions(preprocessorOptions, fortranOptions);

Expand Down
7 changes: 7 additions & 0 deletions flang/test/Flang-Driver/driver-help-hidden.f90
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,21 @@
! CHECK-NEXT: -c Only run preprocess, compile, and assemble steps
! CHECK-NEXT: -D <macro>=<value> Define <macro> to <value> (or 1 if <value> omitted)
! CHECK-NEXT: -E Only run the preprocessor
! CHECK-NEXT: -falternative-parameter-statement
! CHECK-NEXT: Enable the old style PARAMETER statement
! CHECK-NEXT: -fbackslash Specify that backslash in string introduces an escape character
! CHECK-NEXT: -fcolor-diagnostics Enable colors in diagnostics
! CHECK-NEXT: -ffixed-form Process source files in fixed form
! CHECK-NEXT: -ffixed-line-length=<value>
! CHECK-NEXT: Use <value> as character line width in fixed mode
! CHECK-NEXT: -ffree-form Process source files in free form
! CHECK-NEXT: -fimplicit-none No implicit typing allowed unless overridden by IMPLICIT statements
! CHECK-NEXT: -finput-charset=<value> Specify the default character set for source files
! CHECK-NEXT: -flogical-abbreviations Enable logical abbreviations
! CHECK-NEXT: -fno-color-diagnostics Disable colors in diagnostics
! CHECK-NEXT: -fopenacc Enable OpenACC
! CHECK-NEXT: -fopenmp Parse OpenMP pragmas and generate parallel code.
! CHECK-NEXT: -fxor-operator Enable .XOR. as a synonym of .NEQV.
! CHECK-NEXT: -help Display available options
! CHECK-NEXT: -I <dir> Add directory to the end of the list of include search paths
! CHECK-NEXT: -module-dir <dir> Put MODULE files in <dir>
Expand Down
14 changes: 14 additions & 0 deletions flang/test/Flang-Driver/driver-help.f90
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,21 @@
! HELP-NEXT: -c Only run preprocess, compile, and assemble steps
! HELP-NEXT: -D <macro>=<value> Define <macro> to <value> (or 1 if <value> omitted)
! HELP-NEXT: -E Only run the preprocessor
! HELP-NEXT: -falternative-parameter-statement
! HELP-NEXT: Enable the old style PARAMETER statement
! HELP-NEXT: -fbackslash Specify that backslash in string introduces an escape character
! HELP-NEXT: -fcolor-diagnostics Enable colors in diagnostics
! HELP-NEXT: -ffixed-form Process source files in fixed form
! HELP-NEXT: -ffixed-line-length=<value>
! HELP-NEXT: Use <value> as character line width in fixed mode
! HELP-NEXT: -ffree-form Process source files in free form
! HELP-NEXT: -fimplicit-none No implicit typing allowed unless overridden by IMPLICIT statements
! HELP-NEXT: -finput-charset=<value> Specify the default character set for source files
! HELP-NEXT: -flogical-abbreviations Enable logical abbreviations
! HELP-NEXT: -fno-color-diagnostics Disable colors in diagnostics
! HELP-NEXT: -fopenacc Enable OpenACC
! HELP-NEXT: -fopenmp Parse OpenMP pragmas and generate parallel code.
! HELP-NEXT: -fxor-operator Enable .XOR. as a synonym of .NEQV.
! HELP-NEXT: -help Display available options
! HELP-NEXT: -I <dir> Add directory to the end of the list of include search paths
! HELP-NEXT: -module-dir <dir> Put MODULE files in <dir>
Expand All @@ -46,15 +53,22 @@
! HELP-FC1-NEXT: -D <macro>=<value> Define <macro> to <value> (or 1 if <value> omitted)
! HELP-FC1-NEXT: -emit-obj Emit native object files
! HELP-FC1-NEXT: -E Only run the preprocessor
! HELP-FC1-NEXT: -falternative-parameter-statement
! HELP-FC1-NEXT: Enable the old style PARAMETER statement
! HELP-FC1-NEXT: -fbackslash Specify that backslash in string introduces an escape character
! HELP-FC1-NEXT: -fdebug-unparse-with-symbols
! HELP-FC1-NEXT: Unparse and stop.
! HELP-FC1-NEXT: -fdebug-unparse Unparse and stop.
! HELP-FC1-NEXT: -ffixed-form Process source files in fixed form
! HELP-FC1-NEXT: -ffixed-line-length=<value>
! HELP-FC1-NEXT: Use <value> as character line width in fixed mode
! HELP-FC1-NEXT: -ffree-form Process source files in free form
! HELP-FC1-NEXT: -fimplicit-none No implicit typing allowed unless overridden by IMPLICIT statements
! HELP-FC1-NEXT: -finput-charset=<value> Specify the default character set for source files
! HELP-FC1-NEXT: -flogical-abbreviations Enable logical abbreviations
! HELP-FC1-NEXT: -fopenacc Enable OpenACC
! HELP-FC1-NEXT: -fopenmp Parse OpenMP pragmas and generate parallel code.
! HELP-FC1-NEXT: -fxor-operator Enable .XOR. as a synonym of .NEQV.
! HELP-FC1-NEXT: -help Display available options
! HELP-FC1-NEXT: -I <dir> Add directory to the end of the list of include search paths
! HELP-FC1-NEXT: -module-dir <dir> Put MODULE files in <dir>
Expand Down
35 changes: 35 additions & 0 deletions flang/test/Flang-Driver/escaped-backslash.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
! Ensure argument -fbackslash works as expected.

! REQUIRES: new-flang-driver

!--------------------------
! FLANG DRIVER (flang-new)
!--------------------------
! RUN: %flang-new -E %s 2>&1 | FileCheck %s --check-prefix=ESCAPED
! RUN: %flang-new -E -fbackslash -fno-backslash %s 2>&1 | FileCheck %s --check-prefix=ESCAPED
! RUN: %flang-new -E -fbackslash %s 2>&1 | FileCheck %s --check-prefix=UNESCAPED

!-----------------------------------------
! FRONTEND FLANG DRIVER (flang-new -fc1)
!-----------------------------------------
! RUN: %flang-new -fc1 -E %s 2>&1 | FileCheck %s --check-prefix=ESCAPED
! RUN: %flang-new -fc1 -E -fbackslash -fno-backslash %s 2>&1 | FileCheck %s --check-prefix=ESCAPED
! RUN: %flang-new -fc1 -E -fbackslash %s 2>&1 | FileCheck %s --check-prefix=UNESCAPED

!-----------------------------------------
! EXPECTED OUTPUT FOR ESCAPED BACKSLASHES
!-----------------------------------------
! ESCAPED:program backslash
! ESCAPED-NEXT:New\\nline
! ESCAPED-NOT:New\nline

!-------------------------------------------
! EXPECTED OUTPUT FOR UNESCAPED BACKSLASHES
!-------------------------------------------
! UNESCAPED:program backslash
! UNESCAPED-NEXT:New\nline
! UNESCAPED-NOT:New\\nline

program Backslash
print *, 'New\nline'
end
10 changes: 10 additions & 0 deletions flang/test/Flang-Driver/frontend-forwarding.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
! Test that flang-new forwards Flang frontend
! options to flang-new -fc1 as expected.

! REQUIRES: new-flang-driver

! RUN: %flang-new -fsyntax-only -### %s -o %t 2>&1 \
! RUN: -finput-charset=utf-8 \
! RUN: | FileCheck %s

! CHECK: "-finput-charset=utf-8"
32 changes: 32 additions & 0 deletions flang/test/Flang-Driver/implicit-none.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
! Ensure argument -fimplicit-none works as expected.

! REQUIRES: new-flang-driver

!--------------------------
! FLANG DRIVER (flang-new)
!--------------------------
! RUN: %flang-new -fsyntax-only %s 2>&1 | FileCheck %s --allow-empty --check-prefix=DEFAULT
! RUN: %flang-new -fsyntax-only -fimplicit-none -fno-implicit-none %s 2>&1 | FileCheck %s --allow-empty --check-prefix=DEFAULT
! RUN: not %flang-new -fsyntax-only -fimplicit-none %s 2>&1 | FileCheck %s --check-prefix=WITH_IMPL_NONE

!-----------------------------------------
! FRONTEND FLANG DRIVER (flang-new -fc1)
!-----------------------------------------
! RUN: %flang-new -fc1 -fsyntax-only %s 2>&1 | FileCheck %s --allow-empty --check-prefix=DEFAULT
! RUN: %flang-new -fc1 -fsyntax-only -fimplicit-none -fno-implicit-none %s 2>&1 | FileCheck %s --allow-empty --check-prefix=DEFAULT
! RUN: not %flang-new -fc1 -fsyntax-only -fimplicit-none %s 2>&1 | FileCheck %s --check-prefix=WITH_IMPL_NONE

!--------------------------------------
! EXPECTED OUTPUT FOR NO IMPLICIT NONE
!--------------------------------------
! DEFAULT-NOT:error

!------------------------------------------
! EXPECTED OUTPUT FOR IMPLICIT NONE ALWAYS
!------------------------------------------
! WITH_IMPL_NONE:No explicit type declared for 'a'
! WITH_IMPL_NONE:No explicit type declared for 'b'

function a()
a = b
end
2 changes: 1 addition & 1 deletion flang/test/Semantics/oldparam02.f90
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
! RUN: not %f18 -falternative-parameter-statement -fdebug-dump-symbols %s 2>&1 | FileCheck %s
! RUN: not %flang -falternative-parameter-statement -fsyntax-only %s 2>&1 | FileCheck %s

! Error tests for "old style" PARAMETER statements
subroutine subr(x1,x2,x3,x4,x5)
Expand Down
2 changes: 1 addition & 1 deletion flang/test/Semantics/resolve64.f90
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
! RUN: %S/test_errors.sh %s %t %f18 -flogical-abbreviations -fxor-operator
! RUN: %S/test_errors.sh %s %t %flang -flogical-abbreviations -fxor-operator

! Like m4 in resolve63 but compiled with different options.
! Alternate operators are enabled so treat these as intrinsic.
Expand Down

0 comments on commit 10826ea

Please sign in to comment.