MSC02-F. Beware of compiler-specific extensions
Compilers may provide specific extensions that enhance functionality and offer convenience for programmers. However, these extensions can significantly compromise code portability and consistency across different development environments, creating challenges for maintenance and future development.
Even when multiple compilers support an extension, its behavior may differ, since the Fortran standard does not govern these features. Consequently, Fortran code relying on compiler-specific extensions cannot be guaranteed to compile or behave consistently across all environments.
This issue is particularly relevant when maintaining or inheriting legacy code. If the original compiler is unsupported or unavailable, developers must port or refactor the code for modern compilers, increasing the complexity of maintenance tasks.
Noncompliant Code Example
In this compliant example, the array z is declared with a size of 3. The DATA statement provides only two values. While some compilers may accept this (e.g., Intel's ifort), it violates the standard requirement that the number of values matches the number of array elements.
program compiler_extension
implicit none
integer :: z(3)
! Noncompliant: z has 3 elements, but only 2 values are provided
data z /1,2/
print *, "z =", z
end program
Compliant Solution
In this compliant solution, the initialization is performed using a standard array constructor in an assignment statement. This ensures the code is portable and the standard explicitly defines the behavior. Alternatively, a DATA statement could be used if exactly three values were provided.
program compliant_extension
implicit none
integer :: z(3)
! Compliant: Explicit assignment matches the aray rank and size.
z = (/1, 2, 3/)
print *, "z =", z
end program
Risk Assessment
Relying on compiler extensions can lead to compilation errors when porting to other platforms.
| Rule | Severity | Likelihood | Detectable | Repairable | Priority | Level |
|---|---|---|---|---|---|---|
| MSC02-F | Medium | Likely | Yes | No | P12 | L1 |
Attachments:
button_arrow_left.png (image/png)
button_arrow_up.png (image/png)
button_arrow_right.png (image/png)


