-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from PlasmaFAIR/add_precision_category
Add precision category
- Loading branch information
Showing
7 changed files
with
133 additions
and
115 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
use crate::parsing::to_text; | ||
use crate::{Method, Rule, Violation}; | ||
use tree_sitter::Node; | ||
/// Defines rules to avoid the 'double precision' and 'double complex' types. | ||
|
||
// TODO rule to prefer 1.23e4_sp over 1.23e4, and 1.23e4_dp over 1.23d4 | ||
|
||
fn double_precision_err_msg(dtype: &str) -> Option<String> { | ||
match dtype { | ||
"double precision" => Some(String::from( | ||
"prefer 'real(real64)' to 'double precision' (see 'iso_fortran_env')", | ||
)), | ||
"double complex" => Some(String::from( | ||
"prefer 'complex(real64)' to 'double complex' (see 'iso_fortran_env')", | ||
)), | ||
_ => None, | ||
} | ||
} | ||
|
||
fn double_precision(node: &Node, src: &str) -> Option<Violation> { | ||
let txt = to_text(node, src)?.to_lowercase(); | ||
if let Some(msg) = double_precision_err_msg(txt.as_str()) { | ||
return Some(Violation::from_node(msg.as_str(), node)); | ||
} | ||
None | ||
} | ||
|
||
pub struct DoublePrecision {} | ||
|
||
impl Rule for DoublePrecision { | ||
fn method(&self) -> Method { | ||
Method::Tree(double_precision) | ||
} | ||
|
||
fn explain(&self) -> &str { | ||
" | ||
The 'double precision' type does not guarantee a 64-bit floating point number | ||
as one might expect. It is instead required to be twice the size of a default | ||
'real', which may vary depending on your system and can be modified by compiler | ||
arguments. For portability, it is recommended to use `real(dp)`, with `dp` set | ||
in one of the following ways: | ||
- `use, intrinsic :: iso_fortran_env, only: dp => real64` | ||
- `integer, parameter :: dp = selected_real_kind(15, 307)` | ||
For code that should be compatible with C, you should instead use | ||
`real(c_double)`, which may be found in the intrinsic module `iso_c_binding`. | ||
" | ||
} | ||
|
||
fn entrypoints(&self) -> Vec<&str> { | ||
vec!["intrinsic_type"] | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::test_utils::test_utils::test_tree_method; | ||
use crate::violation; | ||
use textwrap::dedent; | ||
|
||
#[test] | ||
fn test_double_precision() -> Result<(), String> { | ||
let source = dedent( | ||
" | ||
double precision function double(x) | ||
double precision, intent(in) :: x | ||
double = 2 * x | ||
end function | ||
subroutine triple(x) | ||
double precision, intent(inout) :: x | ||
x = 3 * x | ||
end subroutine | ||
function complex_mul(x, y) | ||
double precision, intent(in) :: x | ||
double complex, intent(in) :: y | ||
double complex :: complex_mul | ||
complex_mul = x * y | ||
end function | ||
", | ||
); | ||
let expected_violations = [ | ||
(2, 1, "double precision"), | ||
(3, 3, "double precision"), | ||
(8, 3, "double precision"), | ||
(13, 3, "double precision"), | ||
(14, 3, "double complex"), | ||
(15, 3, "double complex"), | ||
] | ||
.iter() | ||
.map(|(line, col, kind)| { | ||
let msg = double_precision_err_msg(kind).unwrap(); | ||
violation!(&msg, *line, *col) | ||
}) | ||
.collect(); | ||
test_tree_method(&DoublePrecision {}, source, Some(expected_violations))?; | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod double_precision; | ||
pub mod kind_suffixes; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
pub mod implicit_typing; | ||
pub mod literal_kinds; | ||
pub mod real_precision; | ||
pub mod star_kinds; |