@@ -76,9 +76,8 @@ Preprocessor::Preprocessor(simplecpp::TokenList& tokens, const Settings& setting
7676
7777namespace {
7878 struct BadInlineSuppression {
79- BadInlineSuppression (std::string file, const int line, std::string msg) : file(std::move(file)), line(line), errmsg(std::move(msg)) {}
80- std::string file;
81- int line;
79+ BadInlineSuppression (const simplecpp::Location& loc, std::string msg) : location(loc), errmsg(std::move(msg)) {}
80+ simplecpp::Location location;
8281 std::string errmsg;
8382 };
8483}
@@ -140,10 +139,11 @@ static bool parseInlineSuppressionCommentToken(const simplecpp::Token *tok, std:
140139 s.isInline = true ;
141140 s.type = errorType;
142141 s.lineNumber = tok->location .line ;
142+ s.column = tok->location .col ;
143143 }
144144
145145 if (!errmsg.empty ())
146- bad.emplace_back (tok->location . file (), tok-> location . line , std::move (errmsg));
146+ bad.emplace_back (tok->location , std::move (errmsg));
147147
148148 std::copy_if (suppressions.cbegin (), suppressions.cend (), std::back_inserter (inlineSuppressions), [](const SuppressionList::Suppression& s) {
149149 return !s.errorId .empty ();
@@ -158,12 +158,13 @@ static bool parseInlineSuppressionCommentToken(const simplecpp::Token *tok, std:
158158 s.isInline = true ;
159159 s.type = errorType;
160160 s.lineNumber = tok->location .line ;
161+ s.column = tok->location .col ;
161162
162163 if (!s.errorId .empty ())
163164 inlineSuppressions.push_back (std::move (s));
164165
165166 if (!errmsg.empty ())
166- bad.emplace_back (tok->location . file (), tok-> location . line , std::move (errmsg));
167+ bad.emplace_back (tok->location , std::move (errmsg));
167168 }
168169
169170 return true ;
@@ -238,6 +239,7 @@ static void addInlineSuppressions(const simplecpp::TokenList &tokens, const Sett
238239 // Add the suppressions.
239240 for (SuppressionList::Suppression &suppr : inlineSuppressions) {
240241 suppr.fileName = relativeFilename;
242+ suppr.fileIndex = tok->location .fileIndex ;
241243
242244 if (SuppressionList::Type::blockBegin == suppr.type )
243245 {
@@ -260,6 +262,7 @@ static void addInlineSuppressions(const simplecpp::TokenList &tokens, const Sett
260262 suppr.lineBegin = supprBegin->lineNumber ;
261263 suppr.lineEnd = suppr.lineNumber ;
262264 suppr.lineNumber = supprBegin->lineNumber ;
265+ suppr.column = supprBegin->column ;
263266 suppr.type = SuppressionList::Type::block;
264267 inlineSuppressionsBlockBegin.erase (supprBegin);
265268 suppressions.addSuppression (std::move (suppr)); // TODO: check result
@@ -271,8 +274,12 @@ static void addInlineSuppressions(const simplecpp::TokenList &tokens, const Sett
271274 }
272275
273276 if (throwError) {
277+ simplecpp::Location loc (tokens.getFiles ());
274278 // NOLINTNEXTLINE(bugprone-use-after-move) - moved only when thrownError is false
275- bad.emplace_back (suppr.fileName , suppr.lineNumber , " Suppress End: No matching begin" );
279+ loc.fileIndex = suppr.fileIndex ;
280+ loc.line = suppr.lineNumber ;
281+ loc.col = suppr.column ;
282+ bad.emplace_back (loc, " Suppress End: No matching begin" );
276283 }
277284 } else if (SuppressionList::Type::unique == suppr.type || suppr.type == SuppressionList::Type::macro) {
278285 // special handling when suppressing { warnings for backwards compatibility
@@ -286,20 +293,31 @@ static void addInlineSuppressions(const simplecpp::TokenList &tokens, const Sett
286293
287294 suppr.thisAndNextLine = thisAndNextLine;
288295 suppr.lineNumber = tok->location .line ;
296+ suppr.column = tok->location .col ;
289297 suppr.macroName = macroName;
290298 suppressions.addSuppression (std::move (suppr)); // TODO: check result
291299 } else if (SuppressionList::Type::file == suppr.type ) {
292300 if (onlyComments)
293301 suppressions.addSuppression (std::move (suppr)); // TODO: check result
294- else
295- bad.emplace_back (suppr.fileName , suppr.lineNumber , " File suppression should be at the top of the file" );
302+ else {
303+ simplecpp::Location loc (tokens.getFiles ());
304+ loc.fileIndex = suppr.fileIndex ;
305+ loc.line = suppr.lineNumber ;
306+ loc.col = suppr.column ;
307+ bad.emplace_back (loc, " File suppression should be at the top of the file" );
308+ }
296309 }
297310 }
298311 }
299312
300- for (const SuppressionList::Suppression & suppr: inlineSuppressionsBlockBegin)
313+ for (const SuppressionList::Suppression & suppr: inlineSuppressionsBlockBegin) {
314+ simplecpp::Location loc (tokens.getFiles ());
315+ loc.fileIndex = suppr.fileIndex ;
316+ loc.line = suppr.lineNumber ;
317+ loc.col = suppr.column ;
301318 // cppcheck-suppress useStlAlgorithm
302- bad.emplace_back (suppr.fileName , suppr.lineNumber , " Suppress Begin: No matching end" );
319+ bad.emplace_back (loc, " Suppress Begin: No matching end" );
320+ }
303321}
304322
305323void Preprocessor::inlineSuppressions (SuppressionList &suppressions)
@@ -312,7 +330,7 @@ void Preprocessor::inlineSuppressions(SuppressionList &suppressions)
312330 ::addInlineSuppressions (filedata->tokens, mSettings , suppressions, err);
313331 }
314332 for (const BadInlineSuppression &bad : err) {
315- error (bad.file , bad.line , bad. errmsg );
333+ error (bad.location , bad.errmsg , simplecpp::Output::ERROR );
316334 }
317335}
318336
@@ -860,7 +878,7 @@ bool Preprocessor::reportOutput(const simplecpp::OutputList &outputList, bool sh
860878 case simplecpp::Output::ERROR:
861879 hasError = true ;
862880 if (!startsWith (out.msg ," #error" ) || showerror)
863- error (out.location . file () , out.location . line , out.msg );
881+ error (out.location , out.msg , out.type );
864882 break ;
865883 case simplecpp::Output::WARNING:
866884 case simplecpp::Output::PORTABILITY_BACKSLASH:
@@ -869,54 +887,80 @@ bool Preprocessor::reportOutput(const simplecpp::OutputList &outputList, bool sh
869887 const std::string::size_type pos1 = out.msg .find_first_of (" <\" " );
870888 const std::string::size_type pos2 = out.msg .find_first_of (" >\" " , pos1 + 1U );
871889 if (pos1 < pos2 && pos2 != std::string::npos)
872- missingInclude (out.location . file (), out. location . line , out.msg .substr (pos1+1 , pos2-pos1-1 ), out.msg [pos1] == ' \" ' ? UserHeader : SystemHeader);
890+ missingInclude (out.location , out.msg .substr (pos1+1 , pos2-pos1-1 ), out.msg [pos1] == ' \" ' ? UserHeader : SystemHeader);
873891 }
874892 break ;
875893 case simplecpp::Output::INCLUDE_NESTED_TOO_DEEPLY:
876894 case simplecpp::Output::SYNTAX_ERROR:
877895 case simplecpp::Output::UNHANDLED_CHAR_ERROR:
878896 hasError = true ;
879- error (out.location . file () , out.location . line , out.msg );
897+ error (out.location , out.msg , out.type );
880898 break ;
881899 case simplecpp::Output::EXPLICIT_INCLUDE_NOT_FOUND:
882900 case simplecpp::Output::FILE_NOT_FOUND:
883901 case simplecpp::Output::DUI_ERROR:
884902 hasError = true ;
885- error (" " , 0 , out.msg );
903+ std::vector<std::string> f;
904+ simplecpp::Location loc (f);
905+ error (loc, out.msg , out.type );
886906 break ;
887907 }
888908 }
889909
890910 return hasError;
891911}
892912
893- void Preprocessor::error (const std::string &filename, unsigned int linenr, const std::string &msg)
913+ static std::string simplecppErrToId (simplecpp::Output::Type type)
914+ {
915+ switch (type) {
916+ case simplecpp::Output::ERROR:
917+ return " preprocessorErrorDirective" ;
918+ case simplecpp::Output::SYNTAX_ERROR:
919+ return " syntaxError" ;
920+ case simplecpp::Output::UNHANDLED_CHAR_ERROR:
921+ return " unhandledChar" ;
922+ case simplecpp::Output::INCLUDE_NESTED_TOO_DEEPLY:
923+ return " includeNestedTooDeeply" ;
924+ // should never occur
925+ case simplecpp::Output::EXPLICIT_INCLUDE_NOT_FOUND:
926+ case simplecpp::Output::FILE_NOT_FOUND:
927+ case simplecpp::Output::DUI_ERROR:
928+ // handled separately
929+ case simplecpp::Output::MISSING_HEADER:
930+ // no handled at all (warnings)
931+ case simplecpp::Output::WARNING:
932+ case simplecpp::Output::PORTABILITY_BACKSLASH:
933+ throw std::runtime_error (" unexpected type" );
934+ }
935+ }
936+
937+ void Preprocessor::error (const simplecpp::Location& loc, const std::string &msg, simplecpp::Output::Type type)
894938{
895939 std::list<ErrorMessage::FileLocation> locationList;
896- if (!filename .empty ()) {
897- std::string file = Path::fromNativeSeparators (filename );
940+ if (!loc. file () .empty ()) {
941+ std::string file = Path::fromNativeSeparators (loc. file () );
898942 if (mSettings .relativePaths )
899943 file = Path::getRelativePath (file, mSettings .basePaths );
900944
901- locationList.emplace_back (file, linenr, 0 );
945+ locationList.emplace_back (file, loc. line , loc. col );
902946 }
903947 mErrorLogger .reportErr (ErrorMessage (std::move (locationList),
904948 mFile0 ,
905949 Severity::error,
906950 msg,
907- " preprocessorErrorDirective " ,
951+ simplecppErrToId (type) ,
908952 Certainty::normal));
909953}
910954
911955// Report that include is missing
912- void Preprocessor::missingInclude (const std::string &filename, unsigned int linenr , const std::string &header, HeaderTypes headerType)
956+ void Preprocessor::missingInclude (const simplecpp::Location& loc , const std::string &header, HeaderTypes headerType)
913957{
914958 if (!mSettings .checks .isEnabled (Checks::missingInclude))
915959 return ;
916960
917961 std::list<ErrorMessage::FileLocation> locationList;
918- if (!filename .empty ()) {
919- locationList.emplace_back (filename, linenr, 0 );
962+ if (!loc. file () .empty ()) {
963+ locationList.emplace_back (loc. file (), loc. line , loc. col );
920964 }
921965 ErrorMessage errmsg (std::move (locationList), mFile0 , Severity::information,
922966 (headerType==SystemHeader) ?
@@ -932,9 +976,13 @@ void Preprocessor::getErrorMessages(ErrorLogger &errorLogger, const Settings &se
932976 std::vector<std::string> files;
933977 simplecpp::TokenList tokens (files);
934978 Preprocessor preprocessor (tokens, settings, errorLogger, Standards::Language::CPP);
935- preprocessor.missingInclude (" " , 1 , " " , UserHeader);
936- preprocessor.missingInclude (" " , 1 , " " , SystemHeader);
937- preprocessor.error (" " , 1 , " #error message" ); // #error ..
979+ simplecpp::Location loc (files);
980+ preprocessor.missingInclude (loc, " " , UserHeader);
981+ preprocessor.missingInclude (loc, " " , SystemHeader);
982+ preprocessor.error (loc, " message" , simplecpp::Output::ERROR);
983+ preprocessor.error (loc, " message" , simplecpp::Output::SYNTAX_ERROR);
984+ preprocessor.error (loc, " message" , simplecpp::Output::UNHANDLED_CHAR_ERROR);
985+ preprocessor.error (loc, " message" , simplecpp::Output::INCLUDE_NESTED_TOO_DEEPLY);
938986}
939987
940988void Preprocessor::dump (std::ostream &out) const
0 commit comments