PTP/photran/documentation/photran6advanced

From Eclipsepedia

Jump to: navigation, search

You are here: PTP > Photran > Documentation > Photran 6.0 Advanced Features

Photran 6.0 Advanced Features

Contents

Introduction

Photran 6.0 includes a number of sophisticated features that are designed to make it easier to write, modify, search, and maintain Fortran code. These include content assist, which can "auto-complete" variable and function names as you type; a declaration view, which can show the leading comments for the selected variable or procedure; Fortran Search, which allows you to find declarations and references to modules, variables, procedures, etc.; and refactorings, which change your source code to improve its design while preserving its behavior.

Enabling Advanced Features

In order to use any of the advanced features described in this document, you must specifically enable them as described below. After you do this, Photran will index your project; that is, it will build a database of what modules, subprograms, etc. are declared in every file in your project. This information will be updated incrementally every time you save a file. Although this process is usually reasonably fast, it may become disruptive when working on very large projects, so it has been disabled by default. Note that the first time your project is indexed, it may take a while, because Photran must analyze every file in your project; after that, it will only index files that have changed (and files that depend on a file that has changed), so it will generally be much faster.

How to Enable Advanced Features

  1. Right-click on your project in the Fortran Projects view
  2. Click on Properties
  3. Expand Fortran General in the list on the left, and click on Analysis/Refactoring (see screenshot below)
  4. Check the "Enable Fortran analysis/refactoring" check box
  5. If you want to enable content assist, the Fortran Declaration view, etc., check those boxes as well
  6. You may also want to set module and include paths at this point (see below)
  7. Click OK

Setting Module and Include Paths

If your source code contains INCLUDE lines or USE lines referencing modules in other files, Photran needs to know where to look in order to find these. It will not figure this out automatically. For each project in which you plan to use refactoring support,

  1. Right-click on your project's folder in the Fortran Projects view
  2. Click on Properties
  3. Expand Fortran General in the list on the left, and click on Analysis/Refactoring
  4. List the folders in which Photran should search for INCLUDE files and modules when refactoring. They will be searched in order from the first folder listed to the last. Subfolders are not searched automatically; you must include them explicitly.
  5. Click OK
Photran-refac-properties.png

Advanced Features and C Preprocessed Code

Starting with Photran 6, C preprocessor directives are supported in Fortran code, as described in the Photran User's Guide. While most of the advanced features described in this manual can handle C preprocessed code, refactorings currently cannot. If you attempt to refactor C preprocessed code, you will receive an error message.

Advanced Editing Features

Content Assist

As you are typing a Fortran program, content assist pops up a list of the subprograms, variables, intrinsics, etc. that are in scope. It is invoked by pressing Ctrl+Space. As you start typing a name, the list is refined based on what you have typed; you can then use the arrow keys to choose from the list and press Enter to complete the definition. Content assist is useful when you don't remember the exact name of a function, or when a function has a long name and you don't want to type all of it. An example is shown below.

To use content assist, you will need to open the project properties, switch to the Fortran Analysis/Refactoring category, and check the "Enable Fortran content assist (Ctrl+space)" box. You will need to close and re-open any Fortran editors before you can start using content assist, however.

Fortran Content Assist

Fortran Declaration View

The Fortran Declaration view displays the statement where a variable, program, or subprogram is declared. If you place the text editor cursor within (or highlight) a variable name or the name of a subroutine in a CALL statement, the Fortran Declaration view will then display the actual declaration. For variables, the declaration view displays the variable declaration as well as a comment regarding the type of variable and other information, such as dimension information for an array as shown in the example below. For subprograms, it displays the FUNCTION or SUBROUTINE statement, including any comments that precede it.

Fortran Declaration View

Hover Tips

Hover tips display the same text as the Fortran Declaration view (described above); however, they display it in a popup window when the mouse cursor is hovered over an identifier.

Fortran hover tips are enabled in a project's analysis/refactoring properties. To turn them on or off, check the box labeled "Enable Fortran Hover Tips" in the project properties under Fortran General > Analysis/Refactoring. (Note that Analysis/Refactoring must also be enabled.)

Fortran Hover Tips

Search and Navigation

Open Declaration

The Open Declaration command allows you to quickly find the declaration of a particular variable or subprogram. In the Fortran editor, click on (or select) an identifier. Then either

  • click on the Navigate menu, and select Open Declaration, or
  • right-click on the identifier, and, and select Open Declaration, or
  • press the F3 key on your keyboard.

This feature is particularly useful when the declaration is in another file. For example, if your program contains a CALL statement which invokes a subroutine imported from a module in another file, invoking Open Declaration on the subroutine name in the CALL statement will open the module in a new editor, and the cursor will be located on the declaration of that subroutine.

Fortran Search

Fortran Search is a syntax-aware, multi-file search tool. Unlike textual searches, Fortran Search "understands" Fortran programs. It is used to search for a particular type of declaration (e.g., only modules, or only subroutines), or it can find all of the uses of a particular variable or subroutine. It also allows to you to limit the scope of the search (e.g., it can search every file in the workspace, or just a particular project).

In order to use Fortran Search, click on the Search menu and select Fortran...

OpenSearch.jpg

This will open the Eclipse Search dialog, and the Fortran tab will be active. There, you can specify several things:

  • The Search for frame allows you to specify whether it should search for subroutines, functions, modules, variables, etc.
  • The Scope frame allows you to specify what file(s) will be searched.
  • The Limit to frame allows you to specify whether you want to find declarations, references, or both.
SearchDeclarations.jpg
Notice that only the declarations of variables/functions were found. Also note the use of the wild-card character(?) in the search box

Wildcards

Wildcards are special symbols used for pattern matching. For more information, click on Help > Help Contents in Eclipse, and navigate to Workbench User Guide > Reference > User interface information > Search.

  • * - represents any number of any symbols. For example, a search for *d would match bad, rescued, d, assembled, etc.
  • ? - represents any one single character. For example, a search for ?ad would match bad, mad, sad, dad, zad, etc.

Regular expressions are an even more advanced pattern matching languages. For more information, click on Help > Help Contents in Eclipse and navigating to Workbench User Guide > Getting started > Basic tutorial > Searching

Refactoring

Introduction

What is Refactoring?

Refactorings are changes to a program that improve its internal design but do not change its behavior. These include minor, coding style changes (like using IMPLICIT NONE statements), code readability improvements (like replacing a variable named N with one called NUM_POINTS), performance improvements (like interchanging loops under certain conditions), and even larger-scale design changes (like moving a procedure from one module to another).

Although these types of changes can be done by hand, making them is often tedious and error-prone. Photran automates many such refactorings. For example, the Refactor > Rename command can automatically locate the declaration(s) and uses of a particular subroutine, and change its name in all of those locations. It is "smart," too; if there is a subroutine named d and a variable named d in a different context, it won't confuse the two. Moreover, before making such a change, Photran will attempt to verify that the change is safe to make. For example, a subroutine A cannot be renamed to B if there is already a variable named B in a context where that subroutine is called.

For more information on refactoring, see M. Fowler, Refactoring: Improving the Design of Existing Code, Addison-Wesley, 1999.

Refactoring in Photran

Most refactorings can be accessed via the Refactor menu in the menu bar, as described below. However, the most common refactorings also have hotkeys (e.g., Alt+Shift+R for Rename; hotkeys are listed in the Refactoring menu next to each command). Also, most refactorings can be accessed by right-clicking in an editor and choosing Refactor from the popup menu.

Some refactorings (such as Introduce Implicit None and Replace Obsolete Operators) can be applied to several files at once. As described below, this involves selecting one or more files in the Fortran Projects view, then right-clicking on any of the selected filenames and choosing Refactor from the popup menu.

  • Clicking on a filename in the Fortran Projects view selects that file (and only that file).
  • Ctrl+click (Command+click on Mac OS X) can be used to select or deselect additional files.
  • To select a range of files, click on one filename, and Shift+click on a later filename; those files and all of the files in between will be selected as well.

Caution: Photran can only refactor free-format Fortran source code. It is not possible to refactor fixed-form code. Make sure that only free-form Fortran files are selected. The Refactor menu may not be available if any of the files are fixed-form or non-Fortran files.

Rename

  • Description: Rename is essentially a "smart" search and replace: It allows you to change the name of a variable, subprogram, etc. It correctly observes scoping and shadowing rules can also rename subprograms and module entities across files.
  • Applies To:
    1. Local variables1,2
    2. Subprograms3 (including external and interface declarations)
    3. Derived types
    4. Module entities (variables and subprograms)
    5. Main programs
    6. Namelists
    7. Common blocks
    8. Block data subprograms
  • Operation:
    1. Click on the name of a local variable, subprogram, etc.
    2. Click Refactor > Rename... The Rename dialog will appear.
      Rename dialog
    3. Enter a new name for the variable/subprogram/etc.
    4. If you are renaming an external subprogram or a subprogram declared in an interface block, you may want to (un)check the box labeled Match external subprograms with interfaces and external declarations. If this is checked, the refactoring will attempt to find all external subprograms, EXTERNAL statements, and subprogram declarations in INTERFACE blocks that have the given name, and they will all be renamed.
    5. Click Preview to see what changes will be made, then click OK to apply them.
  • Example:
    Simple example of the Rename refactoring

1. Dummy subprogram arguments cannot be renamed
2. Components of derived types cannot be renamed
3. Intrinsic subprograms and type-bound procedures (Fortran 2003) cannot be renamed

Encapsulate variable

  • Description: Encapsulate variable creates getter and setter methods for the selected variable in the module where it is defined and changes variable's visibility to private. It also replaces all uses of that variable in all files to use getter and setter method calls1.
  • Applies To:
    1. Variables defined in the module.
    2. Variables of all basic as well as user-defined types
  • Does Not Apply To:
    1. Arrays
    2. Variables that are not defined in a module
    3. Parameters (i.e. integer, parameter :: CANNOT_ENCAPSULATE)
    4. Pointers (i.e. real, pointer :: CANNOT_ENCAPSULATE)
    5. Targes (i.e. integer, target :: CANNOT_ENCAPSULATE)
  • Operation:
    1. Click on or select the name of variable you want to encapsulate.
    2. Click Refactor > Encapsulate Variable. The Encapsulate Variable dialog will appear.
      Getter and setter name menu for encapsulating variable temp
    3. Enter names for getter and setter methods. You will be warned if the names that you want to assign to your getter and setter methods will be conflicting with some other identifier in any of the involved files.
    4. Click Preview to see what changes will be made, then click OK to apply them.
  • Example:
    Simple example of Encapsulate Variable refactoringtemp
               
    Simple example of Encapsulate Variable refactoringtemp

1. If a variable is used as a parameter to a function/subroutine call, and that function changes the value of the variable as a side-effect, that change will not be preserved.

Interchange loops

  • Description: Swaps inner and outer loops of the selected nested do-loop1. This refactoring merely switches the inner and outer do-headers. It will not make any changes to the body of the loop.
  • Applies To: Selected nested do-loop
  • Operation:
    1. Select the nested loops you wish to interchange
    2. Click Refactor > Interchange Loops. The Interchange loops dialog will appear.
    3. Click Preview to see what changes will be made, then click OK to apply them.
  • Example:
    Simple example of Interchange Loops refactoring

1. In order for refactoring to work correctly, there must be no statements before the second loop. If such statements exist, correctness of the refactoring is not guaranteed.


Introduce Implicit None

  • Description: Adds IMPLICIT NONE statements to a file and adds explicit declarations for all variables that were previously declared implicitly.
  • Applies To: All main programs, subprograms, and modules in one or more files.
  • Operation:
    1. This is a multiple-file refactoring.
      • To Introduce Implicit None in a single file, open the file in the editor and choose Refactor > Introduce Implicit None from the menu bar.
      • To Introduce Implicit None in multiple files, select the files in the Fortran Projects view, right-click on any of the selected filenames, and choose Refactor > Introduce Implicit None from the popup menu.
    2. Click Preview to see what changes will be made, then click OK to apply them.
  • Example:
    Example of the Introduce Implicit None refactoring

Move Saved Variables to Common Block

  • Description: Move Saved Variables to Common Block creates a common block for all "saved" variables of a subprogram. Declarations of these variables in the subprogram are transformed such that they are no longer "saved". The generated common block is declared both in the main PROGRAM and in the affected subprogram. Variables placed in the common block are renamed such that they do not conflict or shadow other variables. The current implementation assumes that the subprogram is in the CONTAINS section of the PROGRAM.
  • Applies To: Subprograms.
  • Operation:
    1. Click on the declaration statement of a subprogram.
    2. Click Refactor > Move Saved Variables to Common Block.
    3. Click Preview to see what changes will be made, then click OK to apply them.
  • Example:
    Example of the Introduce Implicit None refactoring

Replace Obsolete Operators

  • Description: Replace Obsolete Operators replaces all uses of old-style comparison operators (such as .LT. and .EQ.) with their newer equivalents (symbols such as < and ==). and adds explicit declarations for all variables that were previously declared implicitly.
  • Applies To: All uses of the following operators in one or more files: .LT. .LE. .EQ. .NE. .GT. .GE.
  • Operation:
    1. This is a multiple-file refactoring.
      • To Replace Obsolete Operators in a single file, open the file in the editor and choose Refactor > Replace Obsolete Operators from the menu bar.
      • To Replace Obsolete Operators in multiple files, select the files in the Fortran Projects view, right-click on any of the selected filenames, and choose Refactor > Replace Obsolete Operators from the popup menu.
    2. Click Preview to see what changes will be made, then click OK to apply them.
  • Example:
    Example of the Replace Obsolete Operators refactoring

Standardize Statements

  • Description: Standardize Statements rewrites all variables declarations, so that
    • there is only one variable declaration per line, and
    • every variable declaration contains a double colon (::).
    This is intended to make the code more readable.
  • Applies To: All main programs, subprograms, and modules in one or more files.
  • Operation:
    1. This is a multiple-file refactoring.
      • To Standardize Statements in a single file, open the file in the editor and choose Refactor > Standardize Statements from the menu bar.
      • To Standardize Statements in multiple files, select the files in the Fortran Projects view, right-click on any of the selected filenames, and choose Refactor > Standardize Statements from the popup menu.
    2. Click Preview to see what changes will be made, then click OK to apply them.
  • Example:
    Example of the Standardize Statements refactoring

Remove Unused Variables

  • Description: Remove Unused Variables removes declarations of local variables that are never used.
  • Applies To: All main programs, subprograms, and modules in one or more files.
  • Operation:
    1. This is a multiple-file refactoring.
      • To Remove Unused Variables in a single file, open the file in the editor and choose Refactor > Remove Unused Variables from the menu bar.
      • To Remove Unused Variables in multiple files, select the files in the Fortran Projects view, right-click on any of the selected filenames, and choose Refactor > Remove Unused Variables from the popup menu.
    2. Click Preview to see what changes will be made, then click OK to apply them.
  • Example:
    Example of the Remove Unused Variables refactoring

Data to Parameter

  • Description: When a variable declared in a DATA statement is intended to be a constant, Data to Parameter can be used to change it to a variable with the PARAMETER attribute. Using the PARAMETER attribute makes it more clear which variables are constants and which ones are not; it can also result in performance gains, since it may allow an optimizing compiler to replace some variable accesses with the constant value.
  • Applies To: All main programs, subprograms, and modules in one or more files.
  • Operation:
    1. This is a multiple-file refactoring.
      • To transform variables declared as data in variables declared with parameter attribute in a single file, open the file in the editor and choose Refactor > Data To Parameter from the menu bar.
      • To transform variables declared as data in variables declared with parameter attribute in multiple files, select the files in the Fortran Projects view, right-click on any of the selected filenames, and choose Refactor > Data To Parameter from the popup menu.
    2. Click Preview to see what changes will be made, then click OK to apply them.
  • Example:
    Example of the Data To Parameter refactoring

Extract Procedure

  • Description: Extract Procedure removes a sequence of statements from a procedure, places them into a new subroutine, and replaces the original statements with a call to that subroutine. Any local variables used by those statements will be passed as parameters to the new procedure. This refactoring is generally used to make long procedures shorter.
  • Applies To: A sequence of one or more action statements inside a procedure or main program.
  • Operation:
    1. Select a sequence of one or more action statements in the editor. Be sure to include the newline following the last statement in the selection.
    2. Click Refactor > Extract Procedure... The Extract Procedure dialog will appear.
      Extract Procedure dialog
    3. Enter a name for the new procedure that will be created.
    4. Click Preview to see what changes will be made, then click OK to apply them.
  • Example:
    Simple example of the Extract Procedure refactoring

Extract Local Variable

  • Description: Extract Local Variable removes a subexpression from a larger expression and assigns that subexpression to a local variable, replacing the original subexpression with a reference to that variable. This refactoring is usually used to eliminate duplicated subexpressions or to introduce explanatory variable names into complex expressions.
  • Caveats: The refactoring will only be allowed to proceed if extracting the subexpression will preserve the associativity and precedence of the original expression. This refactoring assumes that the extracted expression has no side effects; it does not check whether moving the computation of the extracted expression will change the behavior of the program.
  • Operation:
    1. Select an expression in the editor.
    2. Click Refactor > Extract Local Variable... The Extract Local Variable dialog will appear.
    3. Enter the type and name for the new local variable that will be created.
    4. Click Preview to see what changes will be made, then click OK to apply them.


Canonicalize Keyword Capitalization

  • Description: Makes all applicable keywords the same case throughout the selected Fortran program files.
  • Applies To: All keywords except those listed below.
  • Does not apply to:
    1. Identifiers
    2. All constants except for integer constants and real constants
  • Operation:
    1. This is a multiple-file refactoring.
      • To Canonicalize Keyword Capitalization in a single file, open the file in the editor and choose Refactor > Canonicalize Keyword Capitalization from the menu bar.
      • To Introduce Implicit None in multiple files, select the files in the Fortran Projects view, right-click on any of the selected filenames, and choose Refactor > Canonicalize Keyword Capitalization from the popup menu.
    2. Select Upper or Lower Case
      Canonicalize keyword capitalization dialog.
    3. Click Preview to see what changes will be made, then click OK to apply them.
  • Example
    Example of Canonicalize Keyword Capitalization refactoring.

Make COMMON Variable Names Consistent

  • Description: Fortran allows different definitions of a COMMON block to give the same variable different names. This is confusing. This refactoring gives the variables the same names in all definitions of the COMMON block.
  • Applies To: All COMMON blocks with a valid name.
  • Does not apply to: COMMON blocks with a NULL name.
  • Operation:
    1. Select the name of the COMMON block in the editor which you wish to make variable names consistent for.
    2. Choose Refactor > Make COMMON Variable Names Consistent form the menu bar.
    3. Enter the new names which you wish to give the COMMON variables. The default new names are the original names in the selected block with "_common" appended.
      Make common var names consistent dialog
    4. Click Preview to see what changes will be made, then click OK to apply them.
  • Example
    Example of Make COMMON variable names consistent refactoring.
    Example of Make COMMON variable names consistent refactoring.

Add ONLY Clause to USE Statement

  • Description: Creates a list of the symbols that are being used from a module, and adds it to the USE statement.
  • Applies To: All modules containing public definitions.
  • Does not apply to: Empty modules or modules with only private entities.
  • Operation:
    1. Select the name of the module in the USE statement you wish to add an ONLY clause to.
    2. Choose Refactor > Add ONLY clause to USE statement.
    3. Select which module entities you wish to include in the ONLY list. Any entities in an existing ONLY list will already be selected and can be deselected to be removed.
      Add ONLY to USE dialog
    4. Click Preview to see what changes will be made, then click OK to apply them.
  • Example
    Example of Add ONLY clause to USE statement refactoring.

Minimize ONLY List

  • Description: Deletes symbols that are not being used from the ONLY list in a USE statement.
  • Applies To: USE statements with an ONLY clause.
  • Operation:
    1. Select the name of the module in the USE statement you wish to minimize the ONLY list for.
    2. Choose Refactor > Minimized ONLY list for Selected module.
    3. Click Preview to see what changes will be made, then click OK to apply them.
  • Example
    Example of Minimize ONLY list refactoring.

Make Private Entity Public

  • Description: Changes a module variable or subprogram from PRIVATE to PUBLIC visibility, and checks that it won't conflict with any existing name where that module is USEd.
  • Applies To: Variables, subroutines, functions.
  • Does Not Apply To: Intrinsics, Externals, Interfaces.
  • Operation:
    1. Select the name of the private entity you wish to make public.
    2. Choose Refactor > Make Private Entity Public.
    3. Click Preview to see what changes will be made, then click OK to apply them.
  • Example
    Example of make private entity public refactoring.

Example 2 of make private entity public refactoring.