| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
package com.puppycrawl.tools.checkstyle.filters; |
| 20 | |
|
| 21 | |
import com.google.common.collect.Lists; |
| 22 | |
import com.puppycrawl.tools.checkstyle.api.AuditEvent; |
| 23 | |
import com.puppycrawl.tools.checkstyle.api.AutomaticBean; |
| 24 | |
import com.puppycrawl.tools.checkstyle.api.FileContents; |
| 25 | |
import com.puppycrawl.tools.checkstyle.api.Filter; |
| 26 | |
import com.puppycrawl.tools.checkstyle.api.TextBlock; |
| 27 | |
import com.puppycrawl.tools.checkstyle.api.Utils; |
| 28 | |
import com.puppycrawl.tools.checkstyle.checks.FileContentsHolder; |
| 29 | |
import java.lang.ref.WeakReference; |
| 30 | |
import java.util.Collection; |
| 31 | |
import java.util.Collections; |
| 32 | |
import java.util.Iterator; |
| 33 | |
import java.util.List; |
| 34 | |
import java.util.regex.Matcher; |
| 35 | |
import java.util.regex.Pattern; |
| 36 | |
import java.util.regex.PatternSyntaxException; |
| 37 | |
import org.apache.commons.beanutils.ConversionException; |
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | 137 | public class SuppressWithNearbyCommentFilter |
| 76 | |
extends AutomaticBean |
| 77 | |
implements Filter |
| 78 | |
{ |
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | 20 | public class Tag implements Comparable<Tag> |
| 83 | |
{ |
| 84 | |
|
| 85 | |
private final String mText; |
| 86 | |
|
| 87 | |
|
| 88 | |
private int mFirstLine; |
| 89 | |
|
| 90 | |
|
| 91 | |
private int mLastLine; |
| 92 | |
|
| 93 | |
|
| 94 | |
private Pattern mTagCheckRegexp; |
| 95 | |
|
| 96 | |
|
| 97 | |
private Pattern mTagMessageRegexp; |
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
public Tag(String aText, int aLine) |
| 107 | |
throws ConversionException |
| 108 | 19 | { |
| 109 | 19 | mText = aText; |
| 110 | |
|
| 111 | 19 | mTagCheckRegexp = mCheckRegexp; |
| 112 | |
|
| 113 | |
|
| 114 | 19 | String format = ""; |
| 115 | |
try { |
| 116 | 19 | format = expandFromComment(aText, mCheckFormat, mCommentRegexp); |
| 117 | 19 | mTagCheckRegexp = Pattern.compile(format); |
| 118 | 19 | if (mMessageFormat != null) { |
| 119 | 2 | format = expandFromComment( |
| 120 | |
aText, mMessageFormat, mCommentRegexp); |
| 121 | 2 | mTagMessageRegexp = Pattern.compile(format); |
| 122 | |
} |
| 123 | 19 | int influence = 0; |
| 124 | 19 | if (mInfluenceFormat != null) { |
| 125 | 19 | format = expandFromComment( |
| 126 | |
aText, mInfluenceFormat, mCommentRegexp); |
| 127 | |
try { |
| 128 | 19 | if (format.startsWith("+")) { |
| 129 | 2 | format = format.substring(1); |
| 130 | |
} |
| 131 | 19 | influence = Integer.parseInt(format); |
| 132 | |
} |
| 133 | 0 | catch (final NumberFormatException e) { |
| 134 | 0 | throw new ConversionException( |
| 135 | |
"unable to parse influence from '" + aText |
| 136 | |
+ "' using " + mInfluenceFormat, e); |
| 137 | 19 | } |
| 138 | |
} |
| 139 | 19 | if (influence >= 0) { |
| 140 | 15 | mFirstLine = aLine; |
| 141 | 15 | mLastLine = aLine + influence; |
| 142 | |
} |
| 143 | |
else { |
| 144 | 4 | mFirstLine = aLine + influence; |
| 145 | 4 | mLastLine = aLine; |
| 146 | |
} |
| 147 | |
} |
| 148 | 0 | catch (final PatternSyntaxException e) { |
| 149 | 0 | throw new ConversionException( |
| 150 | |
"unable to parse expanded comment " + format, |
| 151 | |
e); |
| 152 | 19 | } |
| 153 | 19 | } |
| 154 | |
|
| 155 | |
|
| 156 | |
public String getText() |
| 157 | |
{ |
| 158 | 0 | return mText; |
| 159 | |
} |
| 160 | |
|
| 161 | |
|
| 162 | |
public int getFirstLine() |
| 163 | |
{ |
| 164 | 0 | return mFirstLine; |
| 165 | |
} |
| 166 | |
|
| 167 | |
|
| 168 | |
public int getLastLine() |
| 169 | |
{ |
| 170 | 0 | return mLastLine; |
| 171 | |
} |
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
|
| 177 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 181 | |
|
| 182 | |
public int compareTo(Tag aOther) |
| 183 | |
{ |
| 184 | 20 | if (mFirstLine == aOther.mFirstLine) { |
| 185 | 0 | return mLastLine - aOther.mLastLine; |
| 186 | |
} |
| 187 | |
|
| 188 | 20 | return (mFirstLine - aOther.mFirstLine); |
| 189 | |
} |
| 190 | |
|
| 191 | |
|
| 192 | |
|
| 193 | |
|
| 194 | |
|
| 195 | |
|
| 196 | |
|
| 197 | |
public boolean isMatch(AuditEvent aEvent) |
| 198 | |
{ |
| 199 | 428 | final int line = aEvent.getLine(); |
| 200 | 428 | if (line < mFirstLine) { |
| 201 | 115 | return false; |
| 202 | |
} |
| 203 | 313 | if (line > mLastLine) { |
| 204 | 290 | return false; |
| 205 | |
} |
| 206 | 23 | final Matcher tagMatcher = |
| 207 | |
mTagCheckRegexp.matcher(aEvent.getSourceName()); |
| 208 | 23 | if (tagMatcher.find()) { |
| 209 | 20 | return true; |
| 210 | |
} |
| 211 | 3 | if (mTagMessageRegexp != null) { |
| 212 | 0 | final Matcher messageMatcher = |
| 213 | |
mTagMessageRegexp.matcher(aEvent.getMessage()); |
| 214 | 0 | return messageMatcher.find(); |
| 215 | |
} |
| 216 | 3 | return false; |
| 217 | |
} |
| 218 | |
|
| 219 | |
|
| 220 | |
|
| 221 | |
|
| 222 | |
|
| 223 | |
|
| 224 | |
|
| 225 | |
|
| 226 | |
private String expandFromComment( |
| 227 | |
String aComment, |
| 228 | |
String aString, |
| 229 | |
Pattern aRegexp) |
| 230 | |
{ |
| 231 | 40 | final Matcher matcher = aRegexp.matcher(aComment); |
| 232 | |
|
| 233 | 40 | if (!matcher.find()) { |
| 234 | |
|
| 235 | 0 | return aString; |
| 236 | |
|
| 237 | |
} |
| 238 | 40 | String result = aString; |
| 239 | 126 | for (int i = 0; i <= matcher.groupCount(); i++) { |
| 240 | |
|
| 241 | 86 | result = result.replaceAll("\\$" + i, matcher.group(i)); |
| 242 | |
} |
| 243 | 40 | return result; |
| 244 | |
} |
| 245 | |
|
| 246 | |
|
| 247 | |
@Override |
| 248 | |
public final String toString() |
| 249 | |
{ |
| 250 | 0 | return "Tag[lines=[" + getFirstLine() + " to " + getLastLine() |
| 251 | |
+ "]; text='" + getText() + "']"; |
| 252 | |
} |
| 253 | |
} |
| 254 | |
|
| 255 | |
|
| 256 | |
private static final String DEFAULT_COMMENT_FORMAT = |
| 257 | |
"SUPPRESS CHECKSTYLE (\\w+)"; |
| 258 | |
|
| 259 | |
|
| 260 | |
private static final String DEFAULT_CHECK_FORMAT = ".*"; |
| 261 | |
|
| 262 | |
|
| 263 | 1 | private static final String DEFAULT_MESSAGE_FORMAT = null; |
| 264 | |
|
| 265 | |
|
| 266 | |
private static final String DEFAULT_INFLUENCE_FORMAT = "0"; |
| 267 | |
|
| 268 | |
|
| 269 | 7 | private boolean mCheckC = true; |
| 270 | |
|
| 271 | |
|
| 272 | 7 | private boolean mCheckCPP = true; |
| 273 | |
|
| 274 | |
|
| 275 | |
private Pattern mCommentRegexp; |
| 276 | |
|
| 277 | |
|
| 278 | |
private String mCheckFormat; |
| 279 | |
|
| 280 | |
|
| 281 | |
private Pattern mCheckRegexp; |
| 282 | |
|
| 283 | |
|
| 284 | |
private String mMessageFormat; |
| 285 | |
|
| 286 | |
|
| 287 | |
private String mInfluenceFormat; |
| 288 | |
|
| 289 | |
|
| 290 | |
|
| 291 | |
|
| 292 | 7 | private final List<Tag> mTags = Lists.newArrayList(); |
| 293 | |
|
| 294 | |
|
| 295 | |
|
| 296 | |
|
| 297 | |
|
| 298 | |
|
| 299 | |
|
| 300 | |
|
| 301 | 7 | private WeakReference<FileContents> mFileContentsReference = |
| 302 | |
new WeakReference<FileContents>(null); |
| 303 | |
|
| 304 | |
|
| 305 | |
|
| 306 | |
|
| 307 | |
|
| 308 | |
|
| 309 | |
public SuppressWithNearbyCommentFilter() |
| 310 | 7 | { |
| 311 | 7 | if (DEFAULT_COMMENT_FORMAT != null) { |
| 312 | 7 | setCommentFormat(DEFAULT_COMMENT_FORMAT); |
| 313 | |
} |
| 314 | 7 | if (DEFAULT_CHECK_FORMAT != null) { |
| 315 | 7 | setCheckFormat(DEFAULT_CHECK_FORMAT); |
| 316 | |
} |
| 317 | 7 | if (DEFAULT_MESSAGE_FORMAT != null) { |
| 318 | 0 | setMessageFormat(DEFAULT_MESSAGE_FORMAT); |
| 319 | |
} |
| 320 | 7 | if (DEFAULT_INFLUENCE_FORMAT != null) { |
| 321 | 7 | setInfluenceFormat(DEFAULT_INFLUENCE_FORMAT); |
| 322 | |
} |
| 323 | 7 | } |
| 324 | |
|
| 325 | |
|
| 326 | |
|
| 327 | |
|
| 328 | |
|
| 329 | |
|
| 330 | |
public void setCommentFormat(String aFormat) |
| 331 | |
throws ConversionException |
| 332 | |
{ |
| 333 | |
try { |
| 334 | 11 | mCommentRegexp = Utils.getPattern(aFormat); |
| 335 | |
} |
| 336 | 0 | catch (final PatternSyntaxException e) { |
| 337 | 0 | throw new ConversionException("unable to parse " + aFormat, e); |
| 338 | 11 | } |
| 339 | 11 | } |
| 340 | |
|
| 341 | |
|
| 342 | |
public FileContents getFileContents() |
| 343 | |
{ |
| 344 | 175 | return mFileContentsReference.get(); |
| 345 | |
} |
| 346 | |
|
| 347 | |
|
| 348 | |
|
| 349 | |
|
| 350 | |
|
| 351 | |
public void setFileContents(FileContents aFileContents) |
| 352 | |
{ |
| 353 | 7 | mFileContentsReference = new WeakReference<FileContents>(aFileContents); |
| 354 | 7 | } |
| 355 | |
|
| 356 | |
|
| 357 | |
|
| 358 | |
|
| 359 | |
|
| 360 | |
|
| 361 | |
public void setCheckFormat(String aFormat) |
| 362 | |
throws ConversionException |
| 363 | |
{ |
| 364 | |
try { |
| 365 | 11 | mCheckRegexp = Utils.getPattern(aFormat); |
| 366 | 11 | mCheckFormat = aFormat; |
| 367 | |
} |
| 368 | 0 | catch (final PatternSyntaxException e) { |
| 369 | 0 | throw new ConversionException("unable to parse " + aFormat, e); |
| 370 | 11 | } |
| 371 | 11 | } |
| 372 | |
|
| 373 | |
|
| 374 | |
|
| 375 | |
|
| 376 | |
|
| 377 | |
|
| 378 | |
public void setMessageFormat(String aFormat) |
| 379 | |
throws ConversionException |
| 380 | |
{ |
| 381 | |
|
| 382 | |
try { |
| 383 | 1 | Utils.getPattern(aFormat); |
| 384 | |
} |
| 385 | 0 | catch (final PatternSyntaxException e) { |
| 386 | 0 | throw new ConversionException("unable to parse " + aFormat, e); |
| 387 | 1 | } |
| 388 | 1 | mMessageFormat = aFormat; |
| 389 | 1 | } |
| 390 | |
|
| 391 | |
|
| 392 | |
|
| 393 | |
|
| 394 | |
|
| 395 | |
|
| 396 | |
public void setInfluenceFormat(String aFormat) |
| 397 | |
throws ConversionException |
| 398 | |
{ |
| 399 | |
|
| 400 | |
try { |
| 401 | 11 | Utils.getPattern(aFormat); |
| 402 | |
} |
| 403 | 0 | catch (final PatternSyntaxException e) { |
| 404 | 0 | throw new ConversionException("unable to parse " + aFormat, e); |
| 405 | 11 | } |
| 406 | 11 | mInfluenceFormat = aFormat; |
| 407 | 11 | } |
| 408 | |
|
| 409 | |
|
| 410 | |
|
| 411 | |
|
| 412 | |
|
| 413 | |
|
| 414 | |
public void setCheckCPP(boolean aCheckCPP) |
| 415 | |
{ |
| 416 | 1 | mCheckCPP = aCheckCPP; |
| 417 | 1 | } |
| 418 | |
|
| 419 | |
|
| 420 | |
|
| 421 | |
|
| 422 | |
|
| 423 | |
public void setCheckC(boolean aCheckC) |
| 424 | |
{ |
| 425 | 1 | mCheckC = aCheckC; |
| 426 | 1 | } |
| 427 | |
|
| 428 | |
|
| 429 | |
public boolean accept(AuditEvent aEvent) |
| 430 | |
{ |
| 431 | 168 | if (aEvent.getLocalizedMessage() == null) { |
| 432 | 0 | return true; |
| 433 | |
} |
| 434 | |
|
| 435 | |
|
| 436 | |
|
| 437 | 168 | final FileContents currentContents = FileContentsHolder.getContents(); |
| 438 | 168 | if (currentContents == null) { |
| 439 | |
|
| 440 | |
|
| 441 | 0 | return true; |
| 442 | |
} |
| 443 | 168 | if (getFileContents() != currentContents) { |
| 444 | 7 | setFileContents(currentContents); |
| 445 | 7 | tagSuppressions(); |
| 446 | |
} |
| 447 | 168 | for (final Iterator<Tag> iter = mTags.iterator(); iter.hasNext();) { |
| 448 | 428 | final Tag tag = iter.next(); |
| 449 | 428 | if (tag.isMatch(aEvent)) { |
| 450 | 20 | return false; |
| 451 | |
} |
| 452 | 408 | } |
| 453 | 148 | return true; |
| 454 | |
} |
| 455 | |
|
| 456 | |
|
| 457 | |
|
| 458 | |
|
| 459 | |
|
| 460 | |
private void tagSuppressions() |
| 461 | |
{ |
| 462 | 7 | mTags.clear(); |
| 463 | 7 | final FileContents contents = getFileContents(); |
| 464 | 7 | if (mCheckCPP) { |
| 465 | 6 | tagSuppressions(contents.getCppComments().values()); |
| 466 | |
} |
| 467 | 7 | if (mCheckC) { |
| 468 | 6 | final Collection<List<TextBlock>> cComments = |
| 469 | |
contents.getCComments().values(); |
| 470 | 6 | for (final List<TextBlock> element : cComments) { |
| 471 | 36 | tagSuppressions(element); |
| 472 | |
} |
| 473 | |
} |
| 474 | 7 | Collections.sort(mTags); |
| 475 | 7 | } |
| 476 | |
|
| 477 | |
|
| 478 | |
|
| 479 | |
|
| 480 | |
|
| 481 | |
|
| 482 | |
private void tagSuppressions(Collection<TextBlock> aComments) |
| 483 | |
{ |
| 484 | 42 | for (final TextBlock comment : aComments) { |
| 485 | 156 | final int startLineNo = comment.getStartLineNo(); |
| 486 | 156 | final String[] text = comment.getText(); |
| 487 | 156 | tagCommentLine(text[0], startLineNo); |
| 488 | 180 | for (int i = 1; i < text.length; i++) { |
| 489 | 24 | tagCommentLine(text[i], startLineNo + i); |
| 490 | |
} |
| 491 | 156 | } |
| 492 | 42 | } |
| 493 | |
|
| 494 | |
|
| 495 | |
|
| 496 | |
|
| 497 | |
|
| 498 | |
|
| 499 | |
|
| 500 | |
private void tagCommentLine(String aText, int aLine) |
| 501 | |
{ |
| 502 | 180 | final Matcher matcher = mCommentRegexp.matcher(aText); |
| 503 | 180 | if (matcher.find()) { |
| 504 | 19 | addTag(matcher.group(0), aLine); |
| 505 | |
} |
| 506 | 180 | } |
| 507 | |
|
| 508 | |
|
| 509 | |
|
| 510 | |
|
| 511 | |
|
| 512 | |
|
| 513 | |
private void addTag(String aText, int aLine) |
| 514 | |
{ |
| 515 | 19 | final Tag tag = new Tag(aText, aLine); |
| 516 | 19 | mTags.add(tag); |
| 517 | 19 | } |
| 518 | |
} |