001package eu.cqse.check.util.clang.visitors;
002
003import java.util.ArrayList;
004import java.util.List;
005
006import org.conqat.lib.commons.collections.Pair;
007
008import eu.cqse.check.CheckTextRegionLocation;
009import eu.cqse.check.framework.core.phase.ECodeViewOption;
010import eu.cqse.clang.CXCursor;
011import eu.cqse.clang.IClangCursorVisitor;
012import eu.cqse.clang.SWIGTYPE_p_CXTranslationUnitImpl;
013
014/**
015 * Base class for {@link IClangCursorVisitor}s which collect findings while
016 * visiting {@link CXCursor}s in Clang syntax tree.
017 */
018public abstract class FindingCollectingClangCursorVisitorBase implements IClangCursorVisitor {
019
020        /**
021         * The result of this visitor, a List of Pairs containing location and message
022         * of findings to be created
023         */
024        private final List<Pair<CheckTextRegionLocation, String>> findings = new ArrayList<>();
025
026        /** The main Clang translation unit for the source file. */
027        protected final SWIGTYPE_p_CXTranslationUnitImpl translationUnit;
028
029        /** The uniform path of the source file. */
030        protected final String uniformPath;
031
032        /** The {@link ECodeViewOption#UNFILTERED} source file text. */
033        protected final String fileText;
034
035        protected FindingCollectingClangCursorVisitorBase(SWIGTYPE_p_CXTranslationUnitImpl translationUnit,
036                        String uniformPath, String fileText) {
037                this.translationUnit = translationUnit;
038                this.uniformPath = uniformPath;
039                this.fileText = fileText;
040        }
041
042        /**
043         * Returns the findings reported so far. The contained {@link Pair}s consist of
044         * the finding {@link CheckTextRegionLocation} and the finding message.
045         */
046        public List<Pair<CheckTextRegionLocation, String>> getFindings() {
047                return findings;
048        }
049
050        /**
051         * Creates a finding for the {@link CheckTextRegionLocation} with the given
052         * message.
053         */
054        protected void reportFinding(CheckTextRegionLocation textRegionLocation, String message) {
055                findings.add(Pair.createPair(textRegionLocation, message));
056        }
057}