|
21 | 21 | import javax.xml.parsers.ParserConfigurationException; |
22 | 22 | import java.io.File; |
23 | 23 | import java.io.IOException; |
| 24 | +import java.util.ArrayList; |
| 25 | +import java.util.Arrays; |
| 26 | +import java.util.Collections; |
24 | 27 | import java.util.List; |
25 | 28 |
|
26 | 29 | public class CppcheckInspection extends LocalInspectionTool { |
@@ -92,21 +95,51 @@ private static String prependIncludeDir(@NotNull String cppcheckOptions, @NotNul |
92 | 95 | return String.format("-I\"%s\" %s", path, cppcheckOptions); |
93 | 96 | } |
94 | 97 |
|
| 98 | + // TODO: get the list of supported extensions from Cppcheck if it provides that information |
| 99 | + // TODO: extend list by extensions configured within CLion |
| 100 | + private final static List<String> supportedCExtensions = new ArrayList<>(Arrays.asList( |
| 101 | + "c", |
| 102 | + "cl")); |
| 103 | + |
| 104 | + private final static List<String> supportedCPPExtensions = new ArrayList<>(Arrays.asList( |
| 105 | + "cc", |
| 106 | + "cp", |
| 107 | + "cpp", |
| 108 | + "c++", |
| 109 | + "cxx", |
| 110 | + "hh", |
| 111 | + "hpp", |
| 112 | + "hxx", |
| 113 | + "tpp", |
| 114 | + "txx")); |
| 115 | + |
| 116 | + private final static List<String> supportedHeaderExtensions = new ArrayList<>(Collections.singletonList( |
| 117 | + "h")); |
| 118 | + |
95 | 119 | private static boolean isCFamilyFile(@NotNull final VirtualFile file) { |
| 120 | + return isCFile(file) || isCPPFile(file) || isHeaderFile(file); |
| 121 | + } |
| 122 | + |
| 123 | + static private boolean isFile(@NotNull final VirtualFile file, @NotNull final List<String> supportedExtensions) |
| 124 | + { |
96 | 125 | final String fileExtension = file.getExtension(); |
97 | 126 | if (fileExtension == null) { |
98 | 127 | return false; |
99 | 128 | } |
100 | 129 |
|
101 | 130 | final String lowerFileExtension = fileExtension.toLowerCase(); |
102 | | - return lowerFileExtension.equals("c") || |
103 | | - lowerFileExtension.equals("cc") || |
104 | | - lowerFileExtension.equals("cp") || |
105 | | - lowerFileExtension.equals("cpp") || |
106 | | - lowerFileExtension.equals("c++") || |
107 | | - lowerFileExtension.equals("cxx") || |
108 | | - lowerFileExtension.equals("h") || |
109 | | - lowerFileExtension.equals("hh") || |
110 | | - lowerFileExtension.equals("hpp"); |
| 131 | + return supportedExtensions.contains(lowerFileExtension); |
| 132 | + } |
| 133 | + |
| 134 | + static private boolean isCFile(@NotNull final VirtualFile file) { |
| 135 | + return isFile(file, supportedCExtensions); |
| 136 | + } |
| 137 | + |
| 138 | + static private boolean isCPPFile(@NotNull final VirtualFile file) { |
| 139 | + return isFile(file, supportedCPPExtensions); |
| 140 | + } |
| 141 | + |
| 142 | + static private boolean isHeaderFile(@NotNull final VirtualFile file) { |
| 143 | + return isFile(file, supportedHeaderExtensions); |
111 | 144 | } |
112 | 145 | } |
0 commit comments